我在 firebase 實時資料庫中有兩個陣列,用于管理用戶關注者和關注者。我已經在我的 vue 應用程式中創建了這個函式,如果用戶單擊另一個用戶的個人資料中的按鈕并將以下內容添加到單擊的用戶的個人資料中,它將添加以下內容:
async followUser() {
await update(ref(db, 'Users/' this.profileKey), {
followers: [store.currentUser.uid]
})
await update(ref(db, 'Users/' store.currentUserKey), {
following: [this.$route.params.uid]
})
}
目前,我已經在兩個組態檔上進行了測驗,我已經創建了 ad-hoc 并且該功能按預期運行,但我對此表示懷疑。如果用戶開始關注其他用戶,新條目是否會正確添加到現有陣列中,還是會覆寫所有資料?將值推送或洗掉到存盤在 firebase 實時資料庫中的陣列中的正確方法是什么?
uj5u.com熱心網友回復:
在 Firebase 實時資料庫 API 中,沒有原子方式可以將專案添加到陣列中或從陣列中洗掉專案。您必須讀取整個陣列,添加專案,然后將陣列寫回。
最好將followersand存盤followings為地圖而不是陣列,其值為true(因為在 Firebase 中你不能有沒有值的鍵。如果你這樣做,添加一個新的用戶關注者將是:
set(ref(db, 'Users/' this.profileKey '/followers/' store.currentUser.uid), true)
洗掉關注將是:
set(ref(db, 'Users/' this.profileKey '/followers/' store.currentUser.uid), null)
或者
remote(ref(db, 'Users/' this.profileKey '/followers/' store.currentUser.uid))
另請參閱:最佳實踐:Firebase 中的陣列。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/486819.html
