【发布时间】:2021-09-15 00:31:06
【问题描述】:
我正在从 firestore 集合中检索一个数组,如下所示:
firestore.collection('data').doc('meta').get()
.then( meta => {
if (meta.exists) { ...
然后我控制台记录返回的数组,它返回正常。 之后,我将另一个元素推送到该数组并尝试更新该字段。
let arr = meta.data().myArray
myArray.push(newEntry)
console.log(myArray, typeof(myArray)) // type = object
firestore.collection('data').doc('meta').update({myArray: myArray})
.then( res => { ...
我在此之前控制台记录 myArray 并查看该数组是否正常。如果我将该字段的值更改为某个“随机字符串”,那么它也会更新并正常工作。所以连接、凭据、集合、文档名称或任何东西都没有问题。
但我收到以下错误:
FirebaseError: Function DocumentReference.update() called with invalid data. Unsupported field value: undefined (found in document data/meta)
请帮忙!
完整代码:
firestore.collection('data').doc('meta').get()
.then( meta => {
if (meta.exists) {
let myArray = meta.data().myArray
if (!myArray) myArray = []
myArray.push(newEntry)
console.log(myArray, typeof(myArray)) // type = object
firestore.collection('data').doc('meta').update({myArray: myArray})
.then( res => {
console.log('success')
}).catch(err => {
console.log(err)
})
}
})
.catch( err => {
console.log(err)
})
【问题讨论】:
标签: javascript arrays firebase google-cloud-firestore