提问者:小点点

如何更新Firestore数组中特定对象值


我正在用FireBase开发一个Vue应用程序。

我的火库结构照片

我想更新ID:5的所有字段

我的代码

import * as fb from '../firebase';
const DOC_ID = "xyz";

fb.db.collection("orders").doc(DOC_ID).update({
    regions: fb.db.FieldValue.arrayUnion({ Id: 5, Name: "newName", Mail: "new@email.com", ContactNo: 
"100000000" })
});

但我犯了错误:

TypeError: Cannot read property 'arrayUnion' of undefined

我试过这些办法,但都不起作用

如何用FireStore更新“对象数组”?

https://firebase.google.com/docs/firestore/manage-data/add-data#update_elements_in_an_array


共1个答案

匿名用户

错误提示FB.DB.FieldValue未定义。这就是为什么你会出现TypeError。

在正式文档中,可以通过以下路径访问arrayUnion函数:firebase.firestore.fieldvalue.arrayUnion

但是,您是从本地文件。。/firebase导入firebase的。

我们需要知道该文件的内容,以便理解firebase.db.fieldvalue未定义的原因。可能您没有在。。/firebase文件中正确导出它。

您的..firebase文件可能正在导出firebase.firestore()的结果。但是,不能从此对象访问FieldValue属性。

示例:

firebase.firestore.FieldValue // This is correct
firebase.firestore().fieldvalue // This is incorrect

我认为您的fb.dbfirebase.firestore()的结果,而不是firebase.firestore的结果