Reputation: 1826
I created a query that is in a service.TS file that shows an items "state" that is based on a logged in user's UID:
getLists(): FirebaseListObservable<any> {
firebase.auth().onAuthStateChanged(function(user) {
if (user) {console.log("blah", firebase.auth().currentUser.uid)
// User is signed in.
}
});
return this.db.list('/foods', {
query: {
orderByChild: 'state/'+firebase.auth().currentUser.uid+'/state',
equalTo: 'listed'
}
});
}
Using this query, I'm getting an error that reads:
Uncaught (in promise): TypeError: null is not an object (evaluating 'WEBPACK_IMPORTED_MODULE_2_firebase_app"auth".currentUser.uid').
Using {console.log("blah", firebase.auth().currentUser.uid)
, which you can see above, correctly displays the UID in the log, confirming that the user is signed in.
I'm not sure if it's relevant but this query is being called by the page's main TS file with:
ngOnInit() {
this.moviesSvc.getLists()
.subscribe(lists => {console.log(lists); console.log("UID", firebase.auth().currentUser.uid); this.lists = lists})
}
What do I need to do to correctly pass the UID to the query?
Upvotes: 0
Views: 2126
Reputation: 100175
onAuthStateChanged() is asynchronous, so when you have the user uid
in its callback, then only perform your operation, like this:
getLists(): FirebaseListObservable<any> {
var self = this;
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
// User is signed in.
var userID = user.uid;
return self.db.list('/foods', {
query: {
orderByChild: 'state/' + userID + '/state',
equalTo: 'listed'
}
});
}
});
}
Upvotes: 3