1 回答

TA貢獻(xiàn)1789條經(jīng)驗(yàn) 獲得超10個贊
onAuthStateChanged()
“添加觀察者以更改用戶的登錄狀態(tài)”。因此它會不斷觀察用戶的登錄狀態(tài)是否發(fā)生變化。
既然你想通過調(diào)用一個函數(shù)來執(zhí)行你的業(yè)務(wù)邏輯,你最好使用屬性currentUser
來獲取用戶值。
以下幾行應(yīng)該可以解決問題:
?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 [];
? ? ? }
? ??
? };
添加回答
舉報(bào)