【问题标题】:How to update an array in firestore [duplicate]如何更新firestore中的数组[重复]
【发布时间】:2019-02-01 23:21:17
【问题描述】:

我有这个数组,里面有一个对象,我想更新said,以便里面有一些文本。

代码:

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

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

但这只会覆盖整个数组。

【问题讨论】:

  • Doug Stevenson 我已经这样做了,但对我没有帮助

标签: javascript firebase google-cloud-firestore


【解决方案1】:

您需要使用设置而不是更新,合并为真,以确保您不会覆盖整个数组,请在此处查看设置文档https://firebase.google.com/docs/firestore/manage-data/add-data

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

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

【讨论】:

  • 它不起作用
  • 再次覆盖
  • 编辑后检查?问题是它确实会擦除整个数组,而您应该指定要为其重写属性的索引
  • 这与 gotRates[0] 不起作用
  • 试试我刚刚编辑的那个?
猜你喜欢
  • 1970-01-01
  • 2020-10-06
  • 2019-05-14
  • 2020-07-27
  • 2022-01-01
  • 1970-01-01
  • 2018-12-09
  • 2020-10-05
  • 2020-03-02
相关资源
最近更新 更多