Firestore FieldValue.arrayUnion not working in a Cloud Function












0















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 !










share|improve this question




















  • 1





    Where's your function trigger? You can't just deploy code to Cloud Functions and run it as if it's a script. You need to define a trigger that gets invoked when some conditions are met.

    – Doug Stevenson
    Nov 23 '18 at 8:22











  • It's an HTTP trigger, and it works fine.

    – Fox5150
    Nov 23 '18 at 8:25






  • 2





    It always helps to see the complete, minimal code that reproduces the problem. You might be doing something else wrong. Also check the log to see if anything shows up there.

    – Doug Stevenson
    Nov 23 '18 at 8:37






  • 2





    @Fox5150 You should send the response only when the Promise returned by update() has resolved, i.e. in a then() function. Have a look at this video from... Doug :-) (youtube.com/…)

    – Renaud Tarnec
    Nov 23 '18 at 8:54








  • 1





    The function is going to terminate and clean up when you send a response. Since you're sending the response before the async call to update completes, you have no guarantee that the async work will complete. Take some time to learn about how Cloud Functions works with respect to async work. firebase.google.com/docs/functions/terminate-functions

    – Doug Stevenson
    Nov 23 '18 at 8:57


















0















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 !










share|improve this question




















  • 1





    Where's your function trigger? You can't just deploy code to Cloud Functions and run it as if it's a script. You need to define a trigger that gets invoked when some conditions are met.

    – Doug Stevenson
    Nov 23 '18 at 8:22











  • It's an HTTP trigger, and it works fine.

    – Fox5150
    Nov 23 '18 at 8:25






  • 2





    It always helps to see the complete, minimal code that reproduces the problem. You might be doing something else wrong. Also check the log to see if anything shows up there.

    – Doug Stevenson
    Nov 23 '18 at 8:37






  • 2





    @Fox5150 You should send the response only when the Promise returned by update() has resolved, i.e. in a then() function. Have a look at this video from... Doug :-) (youtube.com/…)

    – Renaud Tarnec
    Nov 23 '18 at 8:54








  • 1





    The function is going to terminate and clean up when you send a response. Since you're sending the response before the async call to update completes, you have no guarantee that the async work will complete. Take some time to learn about how Cloud Functions works with respect to async work. firebase.google.com/docs/functions/terminate-functions

    – Doug Stevenson
    Nov 23 '18 at 8:57
















0












0








0








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 !










share|improve this question
















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 !







arrays firebase google-cloud-firestore google-cloud-functions






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 8:52









Renaud Tarnec

12.8k21734




12.8k21734










asked Nov 23 '18 at 8:16









Fox5150Fox5150

1,3391117




1,3391117








  • 1





    Where's your function trigger? You can't just deploy code to Cloud Functions and run it as if it's a script. You need to define a trigger that gets invoked when some conditions are met.

    – Doug Stevenson
    Nov 23 '18 at 8:22











  • It's an HTTP trigger, and it works fine.

    – Fox5150
    Nov 23 '18 at 8:25






  • 2





    It always helps to see the complete, minimal code that reproduces the problem. You might be doing something else wrong. Also check the log to see if anything shows up there.

    – Doug Stevenson
    Nov 23 '18 at 8:37






  • 2





    @Fox5150 You should send the response only when the Promise returned by update() has resolved, i.e. in a then() function. Have a look at this video from... Doug :-) (youtube.com/…)

    – Renaud Tarnec
    Nov 23 '18 at 8:54








  • 1





    The function is going to terminate and clean up when you send a response. Since you're sending the response before the async call to update completes, you have no guarantee that the async work will complete. Take some time to learn about how Cloud Functions works with respect to async work. firebase.google.com/docs/functions/terminate-functions

    – Doug Stevenson
    Nov 23 '18 at 8:57
















  • 1





    Where's your function trigger? You can't just deploy code to Cloud Functions and run it as if it's a script. You need to define a trigger that gets invoked when some conditions are met.

    – Doug Stevenson
    Nov 23 '18 at 8:22











  • It's an HTTP trigger, and it works fine.

    – Fox5150
    Nov 23 '18 at 8:25






  • 2





    It always helps to see the complete, minimal code that reproduces the problem. You might be doing something else wrong. Also check the log to see if anything shows up there.

    – Doug Stevenson
    Nov 23 '18 at 8:37






  • 2





    @Fox5150 You should send the response only when the Promise returned by update() has resolved, i.e. in a then() function. Have a look at this video from... Doug :-) (youtube.com/…)

    – Renaud Tarnec
    Nov 23 '18 at 8:54








  • 1





    The function is going to terminate and clean up when you send a response. Since you're sending the response before the async call to update completes, you have no guarantee that the async work will complete. Take some time to learn about how Cloud Functions works with respect to async work. firebase.google.com/docs/functions/terminate-functions

    – Doug Stevenson
    Nov 23 '18 at 8:57










1




1





Where's your function trigger? You can't just deploy code to Cloud Functions and run it as if it's a script. You need to define a trigger that gets invoked when some conditions are met.

– Doug Stevenson
Nov 23 '18 at 8:22





Where's your function trigger? You can't just deploy code to Cloud Functions and run it as if it's a script. You need to define a trigger that gets invoked when some conditions are met.

– Doug Stevenson
Nov 23 '18 at 8:22













It's an HTTP trigger, and it works fine.

– Fox5150
Nov 23 '18 at 8:25





It's an HTTP trigger, and it works fine.

– Fox5150
Nov 23 '18 at 8:25




2




2





It always helps to see the complete, minimal code that reproduces the problem. You might be doing something else wrong. Also check the log to see if anything shows up there.

– Doug Stevenson
Nov 23 '18 at 8:37





It always helps to see the complete, minimal code that reproduces the problem. You might be doing something else wrong. Also check the log to see if anything shows up there.

– Doug Stevenson
Nov 23 '18 at 8:37




2




2





@Fox5150 You should send the response only when the Promise returned by update() has resolved, i.e. in a then() function. Have a look at this video from... Doug :-) (youtube.com/…)

– Renaud Tarnec
Nov 23 '18 at 8:54







@Fox5150 You should send the response only when the Promise returned by update() has resolved, i.e. in a then() function. Have a look at this video from... Doug :-) (youtube.com/…)

– Renaud Tarnec
Nov 23 '18 at 8:54






1




1





The function is going to terminate and clean up when you send a response. Since you're sending the response before the async call to update completes, you have no guarantee that the async work will complete. Take some time to learn about how Cloud Functions works with respect to async work. firebase.google.com/docs/functions/terminate-functions

– Doug Stevenson
Nov 23 '18 at 8:57







The function is going to terminate and clean up when you send a response. Since you're sending the response before the async call to update completes, you have no guarantee that the async work will complete. Take some time to learn about how Cloud Functions works with respect to async work. firebase.google.com/docs/functions/terminate-functions

– Doug Stevenson
Nov 23 '18 at 8:57














1 Answer
1






active

oldest

votes


















0














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...






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53442871%2ffirestore-fieldvalue-arrayunion-not-working-in-a-cloud-function%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    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...






    share|improve this answer




























      0














      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...






      share|improve this answer


























        0












        0








        0







        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...






        share|improve this answer













        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...







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '18 at 9:24









        Fox5150Fox5150

        1,3391117




        1,3391117
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53442871%2ffirestore-fieldvalue-arrayunion-not-working-in-a-cloud-function%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini