如何在Firestore中更新阵列

时间:2018-08-27 12:06:07

标签: javascript firebase google-cloud-firestore

我的数组中有一个对象,我想更新said,以便在其中添加一些文本。

enter image description here

代码:

ratePost (postId, postLikes) {
  let docId = `${this.currentUser.uid}`

  fb.usersCollection.doc(docId).update({
    gotRates: [
      { said: postLikes }
    ]
  })
  .then(() => {
    console.log('work')
  })
}

但这只会覆盖整个数组。

1 个答案:

答案 0 :(得分:2)

您需要使用set而不是update,将merge设为true,以确保不覆盖整个数组,请在https://firebase.google.com/docs/firestore/manage-data/add-data此处查看set文档。

ratePost (postId, postLikes) {
  let docId = `${this.currentUser.uid}/gotRates/0`

  fb.usersCollection.doc(docId).update({
    said: postLikes
  })
  .then(() => {
    console.log('work')
  })
}
相关问题