我有一個收藏
/userFeed
當當前用戶開始關注/取消關注檔案時,我在哪里創建/洗掉檔案(代表用戶)。
...
/userFeed (C)
/some-followed-user (D)
-date <timestamp>
-interactions <number>
當用戶喜歡一個帖子時,該interactions欄位將被更新。但是......如果用戶不關注帖子所有者怎么辦?然后,我只需要跳過檔案更新,而不必產生失敗/錯誤。
const currentUserFeedRef = firestore
.collection("feeds")
.doc(currentUserId)
.collection("userFeed")
.doc(otherUserId);
const data = {
totalInteractions: admin.firestore.FieldValue.increment(value),
};
const precondition = {
exists: false, // I am trying weird things
};
if (batchOrTransaction) {
return batchOrTransaction.update(
currentUserFeedRef,
data,
precondition
);
}
是否可以“如果檔案不存在則跳過更新”?
uj5u.com熱心網友回復:
是否可以“如果檔案不存在則跳過更新”?
不,不是你解釋的方式。Firestore 更新不會靜默失敗。
如果您需要在更新之前知道檔案是否存在,您應該先閱讀它并檢查它是否存在。您可以在事務中非常輕松地執行此操作,并且如果您首先使用事務物件以這種方式檢查,則可以確保更新不會由于缺少檔案而失敗。
事實上,您正在嘗試做的事情在檔案中作為第一個示例進行了說明。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/475467.html
標籤:javascript 火力基地 谷歌云火库
