2
votes

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)
1
Could you explain why your first parameter is called context and the second one is called event? The names don't seem to match what you're actually going to receive for an onWrite callback. - Doug Stevenson
If you're using the latest version of firebase cloud function(1.0) then onWrite should have (change, context) see the firebase documentation firebase.google.com/docs/functions/…. - hazelcodes
I have just updated my question. - A. Newbie

1 Answers

4
votes
exports.afterCourseAdded = functions.database.ref('/course/{courseId}')
.onWrite((change, context) => {

    // Leave this part out if you want this trigger to work for updates
    if (change.before.exists()) {
        return null;
    }
    // Exit when the data is deleted.
    if (!change.after.exists()) { //This checks if its a delete event
       return null;
    }
    // Grab the current value of what was written to the Realtime Database
    const writtenContent = change.after.val();
    const uid = writtenContent.uid;//This line assumes that the uid field already exists in the node
    const courseId = context.params.courseId;
    console.log('User ' + writtenContent.email + ' created course' + courseId);
    const promise1 = admin.database().ref('/course/' + courseId + '/lect').set(uid);
    return Promise.all([promise1]);
});