從 firebase 獲取資料很好,但是當我發送資料時,我希望最后一個資料在陣列的最后一項之后,但我不知道默認順序是什么,但順序不是我想要的:
[a,b,bc] 當我發送c和d時,它的資料從 firebase 獲取,它看起來像
[c,a,b,bc] 或 [c,a,d,b,bc] 就像我不明白的默認順序
我想要像:
[a,b,bc,c] 或 [a,b,bc,c,d]
從 Firebase 獲取資料
db.collection("messages").onSnapshot((snap) => {
let messages = [];
snap.docs.forEach((doc) => {
messages.push({ ...doc.data(), id: doc.id });
});
console.log(messages);
setmessge(messages);
});
將資料發送到 Firebase
const dataInput = {
date_time: new Date().toLocaleString(),
message,
sender_id: userId,
};
db.collection("messages").add(dataInput);
uj5u.com熱心網友回復:
Firebase 檔案清楚地說明了如何根據ID升序回傳匹配的查詢檔案:
默認情況下,查詢按檔案 ID 升序檢索滿足查詢的所有檔案。
您可以orderBy在檢索資料時在集合參考上指定一個條件,以根據條件對其進行排序:
db.collection("messages")
.orderBy("date_time")
.onSnapshot((snap) => {
// ...
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/357752.html
