Firebase update of an array field fails. Saying that undefined array has been passed, yet the array passed is fine

Viewed 74

I am retrieving an array from firestore collection, like so:

firestore.collection('data').doc('meta').get()
               .then( meta => {
                  if (meta.exists) { ...

Then I console log the returned array and it returns fine. Afterward, I push another element to that array and try to update the field.

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 => { ...

I console log myArray before this and see that the array is fine. If I change the value of the field to some 'randomstring', then it also updates and works fine. So there is no problem in connection, credentials, collection, doc names or anything.

But I get the following error:

FirebaseError: Function DocumentReference.update() called with invalid data. Unsupported field value: undefined (found in document data/meta)

Please, help!

Full code:

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)
})
1 Answers

The issue was caused because there was an object inside of the array which had a key-value pair that was undefined.

Related