0

I am trying to upload an array from my application to Firestone. However, it continues to show

"firebaseError: function fieldvalue.arrayUnion()" called with invalid data.

Here is my code:

    const finalList = this.state.players.map(player => {
        if(player.Present === true){
            return player.id
        }
    })
    finalList.map( id => 
        dbh.collection("Groups").doc(this.state.group)
        .collection('Enrolled').doc('ids').update({
        players: firebase.firestore.FieldValue.arrayUnion(id)
        })
    )

Note that the array "players" does not exist in the database yet.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
kerim Erkan
  • 171
  • 1
  • 2
  • 10
  • Try wrapping id and player in parenthesis. Something like `(player) => {}` and `(id)=>{}` – frunkad Jul 18 '20 at 11:02
  • hi, thank you for your response, unfortunately that did not work – kerim Erkan Jul 18 '20 at 11:06
  • I assume finalList is a list of `id`s - why not simply send it instead of a map function? – frunkad Jul 18 '20 at 11:08
  • `dbh.collection("Groups").doc(this.state.group) .collection('Enrolled').doc('ids').update({ players: firebase.firestore.FieldValue.arrayUnion(finalList) }) ` – frunkad Jul 18 '20 at 11:09
  • unfortunatly this is yielding a similar error "function.field value.arrayUnion() called with invalid data. nested arrays are not supported. i believe that in this case it interprets it as placing an array in an element of array players. – kerim Erkan Jul 18 '20 at 11:25
  • What does a console.log of finalList say? – frunkad Jul 18 '20 at 11:36
  • Have you tried using set with merge true – Shaurya Vardhan Singh Jul 18 '20 at 11:51
  • @frunkad it shows the array of ids – kerim Erkan Jul 18 '20 at 12:28
  • @ShauryaVardhanSingh no, how can i do that? – kerim Erkan Jul 18 '20 at 12:28
  • @kerimErkan you can use set as doc('ids').set(mergedFinalList, {merge: true}) but to use this you need to do some manual additions of elements in array so here i am assuming mergedFinalList is array obtained after adding elements of finalList to original array stored in firebase and for more difference in set with merge and update you can check this question https://stackoverflow.com/questions/46597327/difference-between-set-with-merge-true-and-update – Shaurya Vardhan Singh Jul 18 '20 at 14:56

2 Answers2

2

You can also use the set with merge approach that i have discussed on comments in question

Other than that you can use

   const finalList = this.state.players.map(player => {
        if(player.Present === true){
            return player.id
        }
    })
    dbh.collection("Groups").doc(this.state.group)
    .collection('Enrolled').doc('ids').update({
    players: firebase.firestore.FieldValue.arrayUnion(...finalList)
    })

you can get the detailed info from answer by Matthew Rideout

1

It seems like your id variable is an array of IDs, while the arrayUnion function expects a repeatable variable.

To convert the type, you can use the ... operation:

finalList.map( id => 
  dbh.collection("Groups").doc(this.state.group).collection('Enrolled').doc('ids').update({
    players: firebase.firestore.FieldValue.arrayUnion(...id)
  })
)

Alternatively, and more universalle, you can can perform the conversion with this:

firebase.firestore.FieldValue.arrayUnion.apply(null, id); 

For more on this, see Gil's answer in this post to the firebase-talk mailing list.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807