【问题标题】:Firebase update of an array field fails. Saying that undefined array has been passed, yet the array passed is fine数组字段的 Firebase 更新失败。说传递了未定义的数组,但是传递的数组没问题
【发布时间】: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


    【解决方案1】:

    这个问题是因为数组内部有一个对象有一个未定义的键值对。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-08
      • 1970-01-01
      • 2022-12-26
      • 1970-01-01
      • 2021-12-26
      相关资源
      最近更新 更多