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 [];
? ? ? }
? ??
? };
添加回答
舉報