尝试获取参数时出现Firebase Cloud Function错误

时间:2019-02-24 20:54:19

标签: node.js firebase firebase-realtime-database google-cloud-functions

我在获取onWrite触发器的参数时遇到麻烦。这是我的功能:

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]);
});

用户正在以这种方式写入数据:
课程
| _课程名称
| _ descr:“一些描述”

在写入数据之后,该函数必须将用户的uid写入为“ lect”属性:
课程
| _课程名称
| _ descr:“一些描述”
| _选择:“某些UID”
我收到的错误:

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)

第19行是:

const courseId = context.params.courseId;

感谢您的帮助。
更新1:
功能:

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]);
});

错误:

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 个答案:

答案 0 :(得分:2)

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]);
});