问题描述
我在service.TS文件中创建了一个查询,该查询显示了基于登录用户的UID的项状态":
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:
未捕获(承诺):TypeError:null不是对象(评估' WEBPACK_IMPORTED_MODULE_2_firebase_app "auth" .currentUser.uid').
Uncaught (in promise): TypeError: null is not an object (evaluating 'WEBPACK_IMPORTED_MODULE_2_firebase_app"auth".currentUser.uid').
使用 {console.log("blah",firebase.auth().currentUser.uid)
正确地在日志中显示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.
我不确定是否相关,但是页面的主要TS文件通过以下方式调用此查询:
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})
}
我需要怎么做才能正确地将UID传递给查询?
What do I need to do to correctly pass the UID to the query?
推荐答案
onAuthStateChanged()是异步的,因此,当用户在其回调中使用 uid
时,则仅执行如下操作:
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'
}
});
}
});
}
这篇关于使用Firebase,即使正在记录firebase.auth().currentUser,它也是一个空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!