1 回答
TA贡献1789条经验 获得超10个赞
onAuthStateChanged()
“添加观察者以更改用户的登录状态”。因此它会不断观察用户的登录状态是否发生变化。
既然你想通过调用一个函数来执行你的业务逻辑,你最好使用属性currentUser
来获取用户值。
以下几行应该可以解决问题:
const getBookmarks = async () => {
var firestore = firebase.firestore();
var userBookmarks = firestore.collection('userBookmarks');
const user = firebase.auth().currentUser;
if (user) {
const docSnapshot = await userBookmarks
.doc(user.uid)
.get();
// Note that you need to use await only in the above line, since it is the only asynchronous operation in your async function
if (docSnapshot.exists) {
bm_array = docSnapshot.data().countries;
} else {
bm_array = [];
}
} else {
// Not sure you should return anything here,
// since you just call the function as getBookmarks(); without using the potential returned value
// Note that you don't return anything when user is not undefined, i.e. in the first part of the if block, above
return [];
}
};
添加回答
举报