Cloud functions context params return undefined

  • A+
Category:Languages

I am using cloud functions to listen on new document created on Firestore.

functions.firestore.document('users/{userId}')         .onCreate((snapshot, context) => {             console.log('params', context.params.userId); }); 

The logs show undefined instead of the wildcarded param.

This start happening from 15th dec 2018 at midnight.

Is this a bug related to an update of firestore/cloud functions ? and how we can bypass this issue?

 


There seems to be a bug in the Firebase Functions SDK or platform currently (15 December 2018).

Work-around:

Update The proper way to access the parent document ID is through change.after.ref.parent.parent.id or snapshot.ref.parent.parent.id. Note the .parent.parent.

If you are expecting parameters with the document IDs, you can probably work around the problem by using the data provided in the first argument to you function.

Here is an example with an onCreate triggered function:

export const myCreateTriggeredFn = firestore   .document("user/{userId}/friends/{friendId}")   .onCreate((snapshot, context) => {      let { userId, friendId } = context.params;      if (typeof userId !== "string" || typeof friendId !== "string") {       console.warn(`Invalid params, expected 'userId' and 'friendId'`, context.params);        userId = snapshot.ref.parent.parent.id;       friendId = snapshot.id;     }      // Continue your logic here...   }); 

And for an onWrite triggered function:

export const myChangeTriggeredFn = firestore   .document("user/{userId}/friends/{friendId}")   .onWrite((change, context) => {      let { userId, friendId } = context.params;      if (typeof userId !== "string" || typeof friendId !== "string") {       console.warn(`Invalid params, expected 'userId' and 'friendId'`, context.params);        userId = change.after.ref.parent.parent.id;       friendId = change.after.id;     }      // Continue your logic here...   }); 

For the sake of completeness and to highlight the bug, both examples shows how you would normally extract the IDs from the context.params and then the added work-around to extract the IDs from the snapshot/change objects.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: