I am having trouble getting the parameters of onWrite trigger. Here is my function:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.afterCourseAdded = functions.database.ref('/course/{courseId}').onWrite((context, event) => {
const uid = event.params.uid;
const courseId = context.params.courseId;
console.log('User ' + event.params.email + ' created course' + courseId);
const promise1 = admin.database().ref('/course/' + courseId + '/lect').set(uid);
return Promise.all([promise1]);
});
Users are writing data this way:
course
|_ Course Name
|_ descr: "some description"
After the data is written, the function must write the uid of the user as "lect" property:
course
|_ Course Name
|_ descr: "some description"
|_ lect: "some UID"
The error I'm getting:
TypeError: Cannot read property 'courseId' of undefined
at exports.afterCourseAdded.functions.database.ref.onWrite (/user_code/index.js:19:36)
at cloudFunctionNewSignature (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:105:23)
at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:135:20)
at /var/tmp/worker/worker.js:779:24
at process._tickDomainCallback (internal/process/next_tick.js:135:7)
Line 19 is:
const courseId = context.params.courseId;
Any help is appreciated.
Update 1:
Function:
exports.afterCourseAdded = functions.database.ref('/course/{courseId}').onWrite((change, context) => {
// Only edit data when it is first created.
if (change.before.exists()) {
return null;
}
// Exit when the data is deleted.
if (!change.after.exists()) {
return null;
}
const uid = context.params.uid;
const courseId = context.params.courseId;
console.log('User ' + context.params.email + ' created course' + courseId);
const promise1 = admin.database().ref('/course/' + courseId + '/lect').set(uid);
return Promise.all([promise1]);
});
Error:
TypeError: Cannot read property 'uid' of undefined
at exports.afterCourseAdded.functions.database.ref.onWrite.context (/user_code/index.js:18:31)
at cloudFunctionNewSignature (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:105:23)
at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:135:20)
at /var/tmp/worker/worker.js:779:24
at process._tickDomainCallback (internal/process/next_tick.js:135:7)
contextand the second one is calledevent? The names don't seem to match what you're actually going to receive for an onWrite callback. - Doug Stevenson(change, context)see the firebase documentation firebase.google.com/docs/functions/…. - hazelcodes