0
votes

I have a function that is fired on a onUpdate trigger from my cloud firestore database. The function not being called at all when I change my database.

I did not deploy the function using firestore CLI, instead I deployed it using the GCP Console.

Here is the function:

exports.NotificationListener = functions
    .firestore
    .document('users/{userId}')
    .onUpdate((change, context) => {
      const userId = context.params.userId.toString();
      const eventId = context.eventId;
      console.log('EventId:' + eventId);
      console.log('Change in:' + userId);
      return 200;
    });

Here is the deployment information from the GCP console (showing the trigger): Deployment Information.

Finally, here is the Cloud Firestore schema: Firestore Schema

I want to monitor any changes to any "USER" in the collection: "/user", hence I am using "user/{userId}".

Why is this function not being called when I change the database ?

EDIT 1

A little information about my environment:

I have my entire project core in a TypeScript file. I have over 40 HTTPS triggered functions that are currently online.

I add a new function in my TS file, then I do a npm run build to compile and get the JS file.

Finally, I go to Google Cloud Console and create a function and choose "Zip Upload" and upload the compiled JS file (obviously, along with the required JSON files for getting Database URL, Authentication etc.)

This approach works perfectly fine, at least for HTTP triggered firestore functions.

Now I repeated the same steps as above for the onUpdate trigger and just instead of choosing HTTP trigger, I chose Cloud Firestore trigger. The trigger information can be found above in the screenshot.

onUpdate is not being fired on DB changes.

EDIT 2

My event trigger function NotificationListener is showing up in the firebase console functions list along with my other 40 HTTPS functions. But it is not being called.

1

1 Answers

0
votes

@doug-stevenson, your answer seems to have disappeared, I am not sure why.

Anyway, I found the reason why it wasn't working.

My firebase database was in project "Placeholder 1" and my GCP functions were in project "Placeholder 2". Now, I was able to update the "Placeholder 1" DB from GCP functions (in "Placeholder 2") using firabse-functions API because I set the DatabaseURL to "Placeholder 1".

But, just setting the DatabaseURL to the desired database doesn't work if you want to LISTEN to the database for changes. You actually need to have the function in the same project otherwise it is not able to subscribe and listen for events.

I think it's a little inconsistent that you can read/write to a DB from different projects, but to listen for events, function needs to be in same project. Or maybe I am missing something fundamental that caused this confusion for me.