Home > front end >  How can I change values in a Firestore document from the .onCreate function and return them to a use
How can I change values in a Firestore document from the .onCreate function and return them to a use

Time:02-26

I'm trying to change/create some values within a firestore document from a firebase cloud function and cant figure it out, probably missing something very easy.

exports.onEnterDetails = functions.firestore.document('accounts/{accountId}')
    .onCreate((snapshot, context) => {
        // grab values from the document
        const values = snap.data();
        // do stuff with the values the user put in
        ...
    })

How can I change the value/create new entries in the same firestore document, and how can i return some of those new entries to the user on the front end (website)?

CodePudding user response:

Your onCreate Cloud Function is trigged asynchronously, after the document was already created in Firestore. There is no way at that point anymore to change the initial data in the document, nor is there any way to return data to the client that created the document.

If you want a synchronous flow where you modify the data before it is written, and can respond to the client, consider implementing a Callable Cloud Function or a HTTP cloud function, both of which are synchronous. In either of these you call the Cloud Function from your application code and pass the data with that call, instead of writing it to Firestore directly. The Cloud Function then makes the call to Firestore and returns the necessary result to the caller.

  • Related