Raphael Castro
Raphael Castro

Reputation: 1148

Why is the firebase cloud function not triggering when document is created at responses

exports.createHook = functions.database.ref('/Responses/{ResponsesId}').onCreate((snap, context) => {
    console.log('triggered')
    var response = snap.val
    console.log(response.text);
});

I've written to firestore and its not triggering, what am I missing?

Here is a picture of my functions panel its clearly deploying to the cloud

Here is a picture of the logs its only logging when the function is built so it isn't executing.

Upvotes: 1

Views: 328

Answers (1)

robsiemb
robsiemb

Reputation: 6374

I have written to firestore and it is not triggering

You may have written to Firestore, but your code is written as a Firebase Realtime Database trigger (it uses functions.database). You need to use Firestore triggers to respond to events in Firestore (alternatively, you need to write your updates into a Realtime Database, not Firestore).

It is very easy to get these confused (they're named so similarly!) but they are not the same and need to be coded differently.

For example, the prototype for a Firestore onCreate trigger should look something like:

exports.createHook = functions.firestore
    .document('Responses/{ResponsesId}')
    .onCreate((change, context) => { .... }

Also in the comment thread I note that you said "onCreate should call every time there is a write to the reference". This is not correct. onCreate should only be called when the document is first written to.

Upvotes: 2

Related Questions