介紹
為了收聽我收藏的所有已被洗掉的檔案,而不下載整個收藏,我正在做與此處評論相同的操作。
而不是完全洗掉檔案,只需添加一個“deleted: true”屬性,然后監聽查詢“db.collection(“cities”).where(“deleted”, “==”, true)”
問題
出于某種原因,我不明白,我 從 Firestore 偵聽器接收到所有“修改”事件作為“添加”。
聊天室洗掉代碼
我正在實作一個帶分頁的聊天串列螢屏,所以我只是在安裝螢屏時獲取舊檔案,并訂閱之后new Date()(安裝螢屏的時間)在每個聊天室檔案上發生的所有更改。
為了“洗掉”聊天室檔案,我正在執行以下操作:
async function deleteChatRoom(chatRoomId, userId) {
const chatRoomRef = firestore.collection("chats").doc(chatRoomId);
const chatRoomDoc = await chatRoomRef.get();
const chatRoomData = chatRoomDoc.data();
const messagesPath = `${chatRoomRef.path}/messages`;
// Check that the user is member of the room
if (!chatRoomData.members[userId]) {
throw chatErrors.notChatRoomMember();
}
// Delete all the chatroom messages
await deleteCollection(messagesPath);
// Delete the chat room
await chatRoomRef.update({
deleted: true, <----
lastMessage: admin.firestore.FieldValue.delete(), // Delete denormalized sensitive data
modifiedAt: admin.firestore.FieldValue.serverTimestamp(), <----
read: admin.firestore.FieldValue.delete(), // Delete denormalized sensitive data
});
}
如您所見,我正在更新“modifiedAt”和“deleted”欄位。
聽眾代碼
有了這個,我想要達到的是能夠收聽new Date()在客戶端之后發生的所有聊天室更改(“lastMessage”欄位更新、“讀取”欄位更新、“洗掉”...),因為我之前評論過。如下:
export function listenMyChats(
startAt = undefined,
onNext,
one rror
) {
const currentUserId = getCurrentUser()?.uid;
if (!currentUserId) {
throw authErrors.authenticationRequired();
}
let query = firestore
.collection("chats")
.where("membersArray", "array-contains", currentUserId)
.orderBy("modifiedAt");
if (startAt) {
query = query.startAt(startAt);
}
return query.onSnapshot(onNext, one rror);
}
Something that has sense to me, as I am avoiding reading the entire collection when using the field "startAt" and "orderBy(modifiedAt)".
Subscribing code
Then, to handle all and subscribe to the chat room changes, I am doing the following:
const handleChatsChanges = async (querySnapshot) => {
const changes = querySnapshot.docChanges();
await Promise.all(
changes.map(async (change) => {
if (change.type === "added") {
// NEW INCOMING CHATS
console.log("Added");
} else if (change.type === "modified") {
// MODIFIED CHATS ("deleted", "read", "lastMessage" fields...)
console.log("Modified");
}
});
);
};
useEffect(() => {
const listener = listenMyChats(
startAfter.current,
handleMyChatsChanges,
handleOnListenMyChatsError
);
return () => {
listener();
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
The main problem I am experiencing is that, instead of getting the document modifications in the "modified" scope, I am getting "added" events... Why is this happening?
I thought that the listeners behavior were to get all the docs that match the query condition (as "added") when subscribing. And that, after this, the "added" scope was only executed when new docs were added to the target collection (matching the query conditions, of course).
So, why am I receiving the docs fields modifications as "added"?
uj5u.com熱心網友回復:
正如上面的評論中所詳述的,問題來自于 Firestore 檔案被修改的事實,即第一個快照中不存在的地方,因為您的偵聽器應用于以下查詢:
firestore
.collection("chats")
.where("membersArray", "array-contains", currentUserId)
.orderBy("modifiedAt");
當您修改它們時,它們由上述查詢回傳,因此它們被added偵聽器視為。
它們會被視為modified好像且僅當它們包含在設定偵聽器后回傳的第一個快照中。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/371501.html
標籤:javascript reactjs firebase google-cloud-firestore
