【问题标题】:update subcollection field in firestore更新 Firestore 中的子集合字段
【发布时间】:2022-01-04 01:23:47
【问题描述】:

我有一个 firestore 集合,想更新它的子集合中的几个字段,这些字段采用数组的形式。我无法做到这一点。 data 参数让我得到了我想要的,然后我将它切片以获取子集合中的最后一个数组。也将其分解为collection('').doc('').collection(''),但没有运气。感谢您的帮助!

async updateDialogueStatus({ state }, data){
    const whoDis =  this.$fire.auth.currentUser
    if (data.dialogue.length > 0) {
      const lastDialogueInArray = data.dialogue.slice(-1)[0];
      this.$fire.firestore.collection('conversations/' + data.id + 'dialogue/' + lastDialogueInArray.messageId).update({
           status: "read",
           readBy: whoDis.email
      })
    } else {}

我收到此错误:“未捕获(承诺)类型错误:_this2.$fire.firestore.collection(...).update 不是函数”

【问题讨论】:

    标签: javascript firebase google-cloud-firestore


    【解决方案1】:

    您正在构建要更新的文档的路径,但随后调用this.$fire.firestore.collection。调用.doc获取正确的对象,可以调用update

    //                   ?                                ?
    this.$fire.firestore.doc('conversations/' + data.id + '/dialogue/' + lastDialogueInArray.messageId).update({
    

    【讨论】:

    • 感谢弗兰克!但我现在收到此错误:未捕获(承诺)FirebaseError:无效的文档参考。文档引用必须有偶数个段,但 conversations/Euawxq9p2sjJFqO7dk7kdialogue/4641227046-4d3fd313-250e-4a09-808f-aa86597a67bb 有 3 个。
    • 当我执行“/dialogue/”时,我得到:未捕获(承诺)FirebaseError:没有要更新的文档:projects/foo-79b43/databases/(默认)/documents/conversations/Euawxq9p2sjJFqO7dk7k/对话/4641227046-4d3fd313-250e-4a09-808f-aa86597a67bb
    • 10 您错过了/,请参阅我在回答中添加的第二个?。 2)所以听起来文件不存在。如果要创建新文档,请使用 set 而不是 update
    • 谢谢@Frank!现在弄清楚为什么它不存在。
    猜你喜欢
    • 2019-06-05
    • 2020-08-25
    • 2019-09-26
    • 2020-03-27
    • 1970-01-01
    • 2022-10-25
    • 1970-01-01
    • 1970-01-01
    • 2020-08-10
    相关资源
    最近更新 更多