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.

10
  • Try wrapping id and player in parenthesis. Something like (player) => {} and (id)=>{} Commented Jul 18, 2020 at 11:02
  • hi, thank you for your response, unfortunately that did not work Commented Jul 18, 2020 at 11:06
  • I assume finalList is a list of ids - why not simply send it instead of a map function? Commented Jul 18, 2020 at 11:08
  • dbh.collection("Groups").doc(this.state.group) .collection('Enrolled').doc('ids').update({ players: firebase.firestore.FieldValue.arrayUnion(finalList) }) Commented Jul 18, 2020 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. Commented Jul 18, 2020 at 11:25

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.