繁体   English   中英

如何更新Firebase文档

[英]How to Update Firebase document

当我尝试更新Firebase文档时,它会在console显示以下错误消息

未处理的承诺拒绝FirebaseError:“函数DocumentReference.update()使用无效数据调用。不支持的字段值:undefined(在字段名中找到)”

我尝试使用console.log()进行调试,我发现更新方法中无法识别变量editedItem

如何解决这个问题

data: () => ({
    editedIndex: -1,
    editedItem: {
      id: 1,
      name: "SSFS",
      description: "XXXX"
    },
    newItem: {
      id: null,
      name: null,
      description: null
    }
  }),


    save() {
      if (this.editedIndex > -1) {
        // Update Category
        // Object.assign(this.desserts[this.editedIndex], this.editedItem);
        db.collection('categories').where('id', '==', this.editedItem.id).get().then( (querySnapshot) => {
          querySnapshot.forEach( (doc) => {
            doc.ref.update({
              name : this.editedItem.name,
              description : this.editedItem.description
            }).then(() => {
              this.$router.push('/categories') 
            })
          })
        })
      }
    }

错误:

未处理的承诺拒绝FirebaseError:“函数DocumentReference.update()使用无效数据调用。不支持的字段值:undefined(在字段名称中找到)

您试图以错误的方式访问数据对象。 您应该将数据功能更改为对象。

而不是:

data: () => ({
editedIndex: -1,
editedItem: {
  id: 1,
  name: "SSFS",
  description: "XXXX"
},
newItem: {
  id: null,
  name: null,
  description: null
}

}),

只需从此实现中删除该函数,并将此数据作为常规对象呈现。

做这个:

data: {
editedIndex: -1,
editedItem: {
  id: 1,
  name: "SSFS",
  description: "XXXX"
},
newItem: {
  id: null,
  name: null,
  description: null
}

},

这将允许您使用'this'关键字访问数据对象,并获得正确的值。

现在你要做的就是更改这些行:

name : this.editedItem.name,
description : this.editedItem.description

进入:

name: this.data.editedItem.name,
description: this.data.editedItem.description

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM