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.
I'm the Google employee working on the incident. There is a known compatibility issue for customers using the SDK when Firestore was in a private alpha and have not upgraded.
Could affected customers who are running their code with an SDK version newer than 0.6.2 respond? If you are running version 0.6.1 you can upgrade to 0.6.2 with no code changes to fix.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With