我正在用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
错误提示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.db
是firebase.firestore()
的结果,而不是firebase.firestore
的结果