Comments (8)
It is not possible to combine the await method and the then-catch method.
Please use one of the two alternatives:
Method 1
startCounterById(id: string) {
this.countQuery
.fetch(id)
.unwrap()
.then((result) => {
console.log('result method 1', result);
this.form.reset();
})
.catch(console.error);
}
Method 2
async startCounterById(id: string) {
try {
const result = await this.countQuery.fetch(id).unwrap();
console.log('result method 2', result);
this.form.reset();
} catch (error) {
console.error(error);
}
}
from ngrx-rtk-query.
It is mandatory to connect to the state in a query/lazyQuery due to its nature, as we are supposed to access the data or state of the load. Nevertheless, we will study the implementation of making it independent so that it is not necessary.
from ngrx-rtk-query.
Fixed in 16.0.0-preview.0, the new signals based version.
from ngrx-rtk-query.
The 'fetch' function is to execute the query, you can see a guide at https://github.com/SaulMoro/ngrx-rtk-query#lazy-queries
Also example of its use in https://github.com/SaulMoro/ngrx-rtk-query/blob/master/src/app/features/lazy/lazy-counter/lazy.component.ts#L72
Example:
this.countQuery
.fetch(id, { preferCacheValue })
.unwrap()
.then((result) => {
console.log('result', result);
this.form.reset();
})
.catch(console.error);
from ngrx-rtk-query.
This is not working for me, all other RTK features work except lazy query. Is there any specific configuration for this to work?
from ngrx-rtk-query.
After a lot of testing, I think I found the problem. Running only fetch() and accessing the result as promised doesn't work, you have to use countQuery.state$
in the view.
To test just remove the use of countQuery.state$
from your example in lazy.component.ts and you will see that fetch() return undefined and break "unwrap()" prop access.
I don't understand why the problem exists. This is the expected behavior? did I get lost in something?
// lazy.component.ts
<div *ngIf="countQuery.state$ | async as countQuery" class="space-y-6">...</div>
// lazy.component.ts (THIS WILL BREAK IF WE DONT USE countQuery.state$)
this.countQuery
.fetch(id, { preferCacheValue })
.unwrap() // BREAK : Uncaught (in promise): TypeError: Cannot read properties of undefined (reading 'unwrap')
.then((result) => {
console.log('result method 1', result);
this.form.reset();
})
.catch(console.error);
from ngrx-rtk-query.
I did another test, performing the "subscription" programmatically, and it works.
It is possible to conclude that whenever we use "lazyQuery" we must subscribe the state. Is this statement correct? And if it's true, I think it's worth updating the docs.
// lazy.component.ts
countQuery = useLazyGetCountByIdQuery();
countQuerySubscription = this.countQuery.state$.subscribe();
from ngrx-rtk-query.
Fixed in 16.0.0-preview.0
from ngrx-rtk-query.
Related Issues (14)
- useLazyQuery HOT 3
- Add Posts Examples from RTK Query docs HOT 2
- useQuery: skip uninitialized status HOT 3
- Move Lib to @reduxjs/redux-toolkit HOT 1
- Support Request: Need Recommendation for DI HOT 2
- Question: Does latest ngrx-rtk-query works for ngrx version 11 and angular 11? HOT 1
- Add RTK 1.7.2 support HOT 2
- Module throwing '"@angular/core/core"' has no exported member HOT 3
- Usage with Angular HttpClient? HOT 3
- OpenAPI generation HOT 2
- Initiate with forceRefetch not working as expected HOT 2
- Incompatibility with ngrx v16 HOT 1
- feat: Uptdated to angular 18 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ngrx-rtk-query.