[英]How can I update the nested array stored in firebase database in javascript?
我想更新commentText數組但是當我這樣編碼時它不起作用
這是我的代碼
firebase.database().ref('posts').orderByChild("creatorId").equalTo(creatorId).on("child_added", (data) => {
data.ref.update({
comments: data.val().comments.commentText.concat(this.input)
})
}
在這里看圖片
creatorId
屬性位於comments
節點下,因此您需要對comments/creatorId
進行排序/過濾:
firebase.database().ref('posts').orderByChild("comments/creatorId").equalTo(creatorId).on("child_added", (data) => {
data.ref.update({
comments: data.val().comments.commentText.concat(this.input)
})
})
下面的舊答案,因為代碼可能仍然有用。
當您對 Firebase 數據庫執行查詢時,可能會有多個結果。
所以快照包含這些結果的列表。
即使只有一個結果,快照也會包含一個結果的列表。
所以你的代碼需要處理這個列表,即使只有一個結果:
firebase.database().ref('posts').orderByChild("creatorId").equalTo(creatorId).once("value", (snapshot) => { snapshot.forEach((data) => { data.ref.update({ comments: data.val().comments.commentText.concat(this.input) }) }) }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.