I have run into a specific code state with Angular 2 and ngrx/store where my application breaks totally (routing not working, data not appearing, route singing back to previous one, and all other kinds of possible disasters), but I am running out of idea how to debug what's happening around the store. The redux dev-tools is a great tool, but it does not seem to be able to catch errors.
I have placed .catch
statements throughout my selectors, effects, and just before .subscribe()
statements, trying out two variants:
return state$.select(state => state.user)
.map(user => user.collection)
.catch((error, caught) => {
console.log('error getCollection');
return caught;
});
and
return state$.select(state => state.user)
.map(user => user.collection)
.catch((error) => {
console.log('error getCollection');
return Observable.throw(error);
});
The console remains silent though. I simply don't believe there would be any other "single source of error" but the store.
This code does not catch error for me. Should it?:
.do(() => {
Observable.throw('fake error');
})
.catch(error => {
console.error(error);
return Observable.throw(error);
});
Can anyone point me at how to debug the observables used throughout the store?
Where Does NgRx Store Data? NgRx stores the application state in an RxJS observable inside an Angular service called Store. At the same time, this service implements the Observable interface.
NgRx is made up of 5 main components - Store, Actions, Reducers, Selectors, and Effects. NgRx uses the Redux concept of unidirectional data flow, where all application data goes through the same lifecycle. This unidirectional data flow makes the application's state more predictable and thus easier to understand.
The NgRX Store imports the state management concepts from Redux and adds to them RxJS to provide an observable means of communication throughout the Store APIs, thus giving Angular developers a familiar experience in developing Angular apps.
use .do() for logging a stream - that will fix it
you will need to add the do operator via an RXJS import.
return state$
.do(() => { // log here } )
.select(state => state.user)
.map(user => user.collection)
.catch((error, caught) => {
console.log('error getCollection');
return caught;
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With