3

I am storing the firebase cloud messaging registration tokens for the users in the firestore users collection and whenever I'm sending a notification, I'm deleting the tokens which are unused. I'm doing this on the cloud function with admin sdk. The code I'm using is-

let removeRegistrationTokens = async function (userId: string, tokens: string[]) {
   await db.collection('users')
        .doc(userId)
        .update("tokens",admin.firestore.FieldValue.arrayRemove(tokens)
        .catch(logError)
}

But this is not working and no value is being deleted from the array. I checked the tokens array and the value passed is correct. If I simply change the code to

.update("tokens",admin.firestore.FieldValue.arrayRemove(tokens[0])

This is deleting the first element in the array from the tokens array in firestore. As per the documentation it should delete all the elements passed to it in the array.

If your document contains an array field, you can use arrayUnion() and arrayRemove() to add and remove elements. arrayUnion() adds elements to an array but only elements not already present. arrayRemove() removes all instances of each given element.

Any help on why could it be happening?

1 Answer 1

6

arrayUnion and arrayRemove don't accept array arguments. If you want to pass multiple values, they need to be specified as individual arguments. You could try using the JavaScript spread operator to expand that array into multiple args.

arrayRemove(...tokens)

See also the API documentation for arrayRemove.

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

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.