Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase cloud function Firestore trigger onWrite not behaving as expected when testing locally

I am testing Firestore triggered Firebase Cloud Function locally in Node using Firebase Functions Shell.

When working with a onWrite trigger and passing a new document by calling updateMonth({foo:'new'}) from the functions shell, I can't get a reference to the new document.

exports.updateMonth = functions.firestore
.document('users/{userId}').onWrite((change, context) => {
    const document = change.after.exists ? change.after.data() : null;
    console.log("change.after.exists", change.after.exists)
    console.log("change.before.exists", change.before.exists)
    const oldDocument = change.before.data();
    console.log("Old:",oldDocument)
    return true
})

When calling with both a before and after to emulate a document update updateMonth({before:{foo:'old'},after:{foo:'new'}}) it works as expected. However, when called with just updateMonth({foo:'new'}), both before and after documents don't seem to exist :

i  functions: Preparing to emulate functions.
Warning: You're using Node.js v8.4.0 but Google Cloud Functions only supports v6.11.5.
+  functions: hi
+  functions: helloWorld
+  functions: updateMonth
firebase > updateMonth({before:{foo:'old'},after:{foo:'new'}})
'Successfully invoked function.'
firebase > info: User function triggered, starting execution
info: change.after.exists true
info: change.before.exists true
info: Old: { foo: 'old' }
info: Execution took 20 ms, user function completed successfully

firebase > updateMonth({foo:'new'})
'Successfully invoked function.'
firebase > info: User function triggered, starting execution
info: change.after.exists false
change.before.exists false
Old: undefined
Execution took 2 ms, user function completed successfully

I'm not sure how I can get a reference to the new document being created. I would expect the change.after.exists to be true in that case.

like image 549
smartexpert Avatar asked Sep 06 '18 00:09

smartexpert


People also ask

How do I trigger a firebase cloud function?

Cloud Firestore function triggersTriggered when a document is written to for the first time. Triggered when a document already exists and has any value changed. Triggered when a document with data is deleted. Triggered when onCreate , onUpdate or onDelete is triggered.

Which type of trigger is bound while creating cloud function in the lab?

Triggers supported in Cloud Functions (2nd gen) All event-driven functions in Cloud Functions (2nd gen) use Eventarc for event delivery. In Cloud Functions (2nd gen), Pub/Sub triggers and Cloud Storage triggers are implemented as particular types of Eventarc triggers.

Which triggers are supported by cloud functions?

GCF supports the following trigger types: HTTP, Cloud Pub/Sub, and other sources like Firebase.

What is context in cloud function?

Cloud Functions passes your handler function the following arguments: eventData : An object representing the event data payload. Its format depends on the event type. context : An object containing metadata about the event. callback : An optional function you can call to signal completion.


1 Answers

With onWrite and onUpdate triggers, you need to declare the "before" and "after" state of the document in all cases. You can't try to simplify the API by omitting the "before" or "after" keys. Try this, for example, if you just want to test document creation:

updateMonth({
    after:{foo:'new'}
})

Also, if you have code that's only interested in document creation, it's likely easier to just write on onCreate trigger rather than trying to figure out the document state in an onWrite. Personally, I avoid onWrite and use the other three, because they're easier to reason about what change was actually made.

like image 171
Doug Stevenson Avatar answered Sep 20 '22 10:09

Doug Stevenson