Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I debug ngrx/store?

Tags:

angular

ngrx

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?

like image 994
user776686 Avatar asked Nov 24 '16 13:11

user776686


People also ask

Where is NgRx store stored?

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.

How does NgRx store work?

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.

Is NgRx store an Observable?

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.


1 Answers

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;
  });
like image 175
danday74 Avatar answered Oct 04 '22 02:10

danday74