0

I am writing a Firebase cloud function that does an array-union operation:

const addedMemberIds = ['sarah', 'simon', 'lena'];
doc.ref.update({
  memberIds: FieldValue.arrayUnion(addedMemberIds)
});

The execution of the update leads to an error:

INVALID_ARGUMENT: Cannot convert an array value in an array value.

1 Answers1

0

The signature of the arrayUnion method looks like this: firebaseFirestore.FieldValue.arrayUnion(...elements: any[]). This means it is expecting an argument list of elements that should go inside the array. But when you provide an array, that array is interpreted as one argument which would lead to the following: ['old-user', ['sarah', 'simon', 'lena']]. This is of course not what we want and it obviously leads to the exception because an array as an element of another array is not supported by Firestore.

Therefore, the solution is just to convert the addedMemberIds array to an argument list like so:

const addedMemberIds = ['sarah', 'simon', 'lena'];
doc.ref.update({
  memberIds: FieldValue.arrayUnion(...addedMemberIds)
});