-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fetch from lazy-query strategy returns undefined instead of a promise #44
Comments
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); |
It is not possible to combine the await method and the then-catch method. 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);
}
} |
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 To test just remove the use of 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); |
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(); |
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. |
Fixed in 16.0.0-preview.0, the new signals based version. |
Fixed in 16.0.0-preview.0 |
I'm submitting a...
Current behavior
When using fetch with the lazy query strategy, undefined is returned. If we use unwrap() from fetch, we will get an exception because fetch returns undefined when it should return a promise to be resolved.
Expected behavior
In the previous topic I basically performed some testing strategies but, I believe that the fetch is not working properly, returning a promise.
Minimal reproduction of the problem with instructions
What is the motivation / use case for changing the behavior?
I'm trying to implement a manual call through RTK, so I need to call when the user clicks on a button in the App. And the lazy query strategy is not working correctly, I believe it's a problem with the fetch that doesn't return a promise and doesn't dispatch an action. The most interesting thing is that normal query (like use...Query) works without any problem.
Environment
Development, testing the tool
The text was updated successfully, but these errors were encountered: