Firebase-functions: Firestore trigger: `context.params` sometimes not populated

Created on 15 Dec 2018  路  5Comments  路  Source: firebase/firebase-functions

I have had a few Cloud Firestore triggers invoked today with empty context.params. This is an intermittent issue, and today is the first time I've seen this happen. I did not deploy any changes to my cloud functions prior to this issue starting.

Version info

firebase-functions: 2.0.5

firebase-tools: 6.1.1

firebase-admin: 6.0.0

Test case

I have verified the bug by logging context.params in one of my Firestore onUpdate triggers using console.log.

functions.firestore.document(this.firestoreRoot + '/ledgers/{ledgerId}/accounts/{accountId}/transactions/{transactionId}')
            .onUpdate((snapshot, {params}) => console.log(params));

To prove the issue, I edit the document /contexts/connections/ledgers/553cb118-b68e-46b2-afd5-0ef508858264/accounts/2af276566a9391a81390164d30b46941/transactions/f82c1c5cfea011f8aedfcfe31e411560 in the Firebase console and observe the logs to record the actual shape of context.params during the trigger invocation.

Were you able to successfully deploy your functions?

I was able to deploy my functions. There were no errors. These functions were deployed weeks ago and have been invoked correctly thousands of times during that period.

Expected behavior

context.params should look like this (and most of the time it does):

{ accountId: '2af276566a9391a81390164d30b46941',
     ledgerId: '553cb118-b68e-46b2-afd5-0ef508858264',
     transactionId: 'f82c1c5cfea011f8aedfcfe31e411560' }

Actual behavior

Today, context.params sometimes looks like this:

{ ledgerId: undefined,
     accountId: undefined,
     transactionId: undefined }

As you can see, the params are undefined, _not_ 'undefined', so I don't think these params are even valid (document ID's must be strings). Something is very wrong here.

firestore bug

Most helpful comment

A workaround that seems to work is to use snapshot.id and snapshot.ref.parent.parent.id, etc. As described here:
https://stackoverflow.com/questions/53792140/cloud-functions-context-params-return-undefined

In change triggers the snapshot is available as the after property, e.g. .onUpdate((change, context) => console.log('ID=' + change.after.id))

Update: to find the ID of the parent document, you must use .parent.parent on the reference.

All 5 comments

I am discovering more and more occurrences of this issue today on various Firestore collections. Needless to say this is a very serious problem for any event driven application which utilises Firestore.

Another repro (I am now also logging snapshot.after.ref.path):

path: 'contexts/connections/ledgers/553cb118-b68e-46b2-afd5-0ef508858264/accounts/2af276566a9391a81390164d30b46941/transactions/1e7aece70d469caa21ac379e7c142f37',
  params: 
   { ledgerId: undefined,
     accountId: undefined,
     transactionId: undefined },

A workaround that seems to work is to use snapshot.id and snapshot.ref.parent.parent.id, etc. As described here:
https://stackoverflow.com/questions/53792140/cloud-functions-context-params-return-undefined

In change triggers the snapshot is available as the after property, e.g. .onUpdate((change, context) => console.log('ID=' + change.after.id))

Update: to find the ID of the parent document, you must use .parent.parent on the reference.

The rollback of the affected jobs is now complete, we expect this to be mitigated for all projects now.

https://status.firebase.google.com/incident/Functions/18044

Hi all, confirming that this was an issue with a rollout on our side, which we have now rolled back. I understand your frustration and apologize about the incident. We will be investigating into the reasons why this occurred. Internal bug reference: 121064658.

There is another issue on this that you've correctly referenced here, so I'm going to close this out. Please follow for updates on #358.

Was this page helpful?
0 / 5 - 0 ratings