![](/img/trans.png)
[英]Firebase - return the value from the onSnapshot event in function
[英]Firebase showing error on onSnapshot if some value is coming from await
我正在構建一個應用程序,我在其中通過 roomid 對帖子和用戶進行分類,同時向用戶和每個帖子添加roomid
屬性,但是同時對屬於特定 roomid 的所有帖子進行 feteching 顯示此錯誤
const getRoomID = async () => {
const collectionRef = collection(db, 'users');
if (user) {
const q = query(collectionRef, where('user', '==', user.uid));
const res = await getDocs(q);
let data;
res.forEach((r) => {
data = r.data();
});
return data.roomid;
}
};
const getPosts = async () => {
const collectionRef = collection(db, 'posts');
if (user) {
const roomid = await getRoomID();
const q = query(
collectionRef,
orderBy('timestamp', 'desc'),
where('roomid', '==', roomid)
);
const unsubscribe = onSnapshot(q, (snapshot) => {
setAllPosts(snapshot.docs.map((doc) => ({...doc.data(), id: doc.id})));
});
return unsubscribe;
}
};
getROOMID
function 獲取當前登錄用戶的roomid
並將其與顯示此錯誤的快照 firebase 一起使用
@firebase/firestore: Firestore (9.13.0): Uncaught Error in snapshot listener: {"code":"failed-precondition","name":"FirebaseError"}
我如何構建此應用程序我正在使用 NextJS + Firebase
此錯誤已在 2022 年 11 月 10 日發布的 9.14.0 版中修復。您應該更新您的 Firebase。請參閱 GitHub上的錯誤報告。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.