I'm working in an Angular 2.3 app using ngrx/store + the redux pattern.
In pulling a list of objects from my store (i.e. big image at the top of pages), I'm getting an undefined error that I'm trying to troubleshoot. It looks like it might be a race condition where the function is resolving prior to the observable. In any case, I'm still getting the result but trying to understand the error:
error_handler.js:50 EXCEPTION: Cannot read property 'filter' of undefined
TypeError: Cannot read property 'filter' of undefined
I have in the constructor (which has private _store: Store
)
objects$: Observable<Object[]> = _store.select(s => s.objects)
and my function is:
selectObject(id: number): Observable<Object> {
return this.objects$
.switchMap( objects => objects.filter( object => object.id === id));
}
The most likely explanation is that the state does not contain an
objects
property (or the property isundefined
in the initial state) and that yourselect
operator is then emittingundefined
:There is no mention in your question of the structure of the state, so there is not much more to say.
To confirm that this is the problem, you could add some logging to the selector: