Fox5150
Fox5150

Reputation: 2199

Firestore FieldValue.arrayUnion not working in a Cloud Function

I try to use the arrayUnion functionality of Firestore to add items to an Array. I have a web application and the code below works fine and the elements are added to MyArray :

firebase.firestore().collection("MyCollection").doc("MyDocument").update({
     MyArray: firebase.firestore.FieldValue.arrayUnion({element1: "qqq", element2: "www"})
});

But when I try to make it working in a cloud function, the code doesn't do anything. Nothing is written in my Firestore Array and there is no error on the cloud function logs.

Initialization :

const admin = require('firebase-admin');
admin.initializeApp();

My cloud function has an HTTP trigger that works fine. The code in the cloud function is :

admin.firestore().collection("MyCollection").doc("MyDocument").update({
     MyArray: admin.firestore.FieldValue.arrayUnion({element1: "qqq", element2: "www"})
});

The same code with a simple element added, in the same HTTP triggered cloud function works fine :

admin.firestore().collection("MyCollection").doc("MyDocument").update({
     element1: "qqq"
});

It seems that admin.firestore.FieldValue.arrayUnion doesn't do anything.

The complete cloud function is deployed fine :

exports.test = functions.https.onRequest((req, res) => {
   admin.firestore().collection("MyCollection").doc("MyDocument").update({

       MyArray: admin.firestore.FieldValue.arrayUnion({element1: "qqq", element2: "www"})

   });
   res.status(200).end();
});

He is my package.json file, all packages are up to date :

{
   "name": "functions",
   "description": "Cloud Functions for Firebase",
   "scripts": {
   "serve": "firebase serve --only functions",
   "shell": "firebase experimental:functions:shell",
   "start": "npm run shell",
   "deploy": "firebase deploy --only functions",
   "logs": "firebase functions:log"
},
"dependencies": {
   "@google-cloud/storage": "^1.7.0",
   "admin": "^1.4.0",
   "firebase": "^5.5.9",
   "firebase-admin": "^6.2.0",
   "firebase-functions": "^2.1.0",
   "firestore": "^1.1.6",
},
"private": true
}

Am I missing something ? Thanks a lot !

Upvotes: 0

Views: 4248

Answers (1)

Fox5150
Fox5150

Reputation: 2199

Thanks to Renaud and Doug, it works fine with a Promise :

exports.test = functions.https.onRequest((req, res) => {

return admin.firestore().collection("MyCollection").doc("MyDocument").update({

    MyArray: admin.firestore.FieldValue.arrayUnion({
        element1: "qqq",
        element2: "www"
    })

   }).then(() => {
    console.log('Write succeeded!');
    res.status(200).end();
   });

});

The function clean up was killing everything, and I did not thought that sending the status 200 was cleaning all the process. But one thing is strange : updating only one element is working fine without a promise, but updating the Array is not working ... This is certainly due to the execution time of a more complex process : the update of an Array...

Upvotes: 1

Related Questions