Reputation: 151
I am getting the error
cannot read property 'subscribe' of null.
This is where the error is happening, this is called by the constructor of its class:
loadItems() {
this.itemService.subscribeItems().subscribe(x => {
this.items = x;
console.log(x);
});
}
It calls this services method, this is where the issue is I think. It may be to do with the return null
subscribeItems(): Observable<Item[]> {
this.session.uid.subscribe(x => {
return this.af.database.list('users/' + x + '/Items')
.map(Item.fromJsonList);
});
return null;
}
the session service holds the uid which is updated when the user logs in: This part works ok.
_uid = new BehaviorSubject<string>('');
uid = this._uid.asObservable();
Upvotes: 1
Views: 17935
Reputation: 33
this error is happening because an observable/a service function is null or returning null.
so whichever it is initialize it first.
eg
Items():Observable<any>
{
let itemToReturn = new Observable();
if(this.itemService.subscribeItems()){
this.itemService.subscribeItems().subscribe(data => itemToReturn.next(data));
}
return itemToReturn;
}
Calling this method or subscribing to it won't error since it always returns an initialized item.
Upvotes: 0
Reputation: 1210
Try this:
subscribeItems(): Observable<Item[]> {
return this.session.uid.subscribe(x => {
return this.af.database.list('users/' + x + '/Items')
.map(Item.fromJsonList => Item.fromJsonList.json());
});
return null;
}
Upvotes: 1