2

I have a cloud function that invoked when a certain event happened. In the function i will get an array of string for example let h:string[] = ["foo","bar","baz"] something similar to this when I try to update an array field inside my document like this names: admin.firestore.FieldValue.arrayUnion(h) it failed to do that and throw that error in the console

Error: 3 INVALID_ARGUMENT: Cannot convert an array value in an array value.
    at Object.exports.createStatusError (/srv/node_modules/grpc/src/common.js:87:15)
    at Object.onReceiveStatus (/srv/node_modules/grpc/src/client_interceptors.js:1188:28)
    at InterceptingListener._callNext (/srv/node_modules/grpc/src/client_interceptors.js:564:42)
    at InterceptingListener.onReceiveStatus (/srv/node_modules/grpc/src/client_interceptors.js:614:8)
    at callback (/srv/node_modules/grpc/src/client_interceptors.js:841:24)

But if i changed the code to this names: admin.firestore.FieldValue.arrayUnion("foo","bar","baz") it works but in my function, i receive an array not the above format so it fails

Here is what I want. I have an array field in the document let call it names. when the function is triggered and I get the array from it. I want to add this array to the existing names array

before adding the array names : ["test","test2"]

after adding the array names : ["test","test2","foo","bar","baz"]

How to fix that? I'm using typescript 3.0.1

5
  • 1
    Not quite following but if admin.firestore.FieldValue.arrayUnion("foo","bar","baz") works then admin.firestore.FieldValue.arrayUnion.apply(null,h) should also work. Commented Aug 30, 2018 at 16:38
  • if names=["test","test2"] and otherArray=["foo","bar","baz"] then names.concat(otherArray) would be ["test","test2","foo","bar","baz"] Commented Aug 30, 2018 at 16:43
  • 1
    The first one worked! Thank you very much you saved my day :) Commented Aug 30, 2018 at 16:50
  • I've written it up as an answer. If it was helpful then please mark my answer as the solution. Commented Aug 30, 2018 at 16:58
  • 1
    Sure! i will mark it Commented Aug 30, 2018 at 16:59

2 Answers 2

5

The arrayUnion function have a array value on definition, you need add your array to this array value, on javascript or type you can use ...(NameArray) to union.

items : any[] = [1,2,4,5,67,8];
admin.firestore.FieldValue.arrayUnion(...items);
Sign up to request clarification or add additional context in comments.

1 Comment

Welcome to SO. It's always useful to add some explanation why your code answers the question.
4

If admin.firestore.FieldValue.arrayUnion("foo","bar","baz") works then we can use a function called apply which exists as an object member on every function... It's a very useful one to know. Apply 'applys' a single array of arguments to the function (rather than comma separated arguments) and invokes it.

Hence the solution is:

admin.firestore.FieldValue.arrayUnion.apply(null,h)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.