首页 > 解决方案 > 尝试获取参数时出现 Firebase Cloud Function 错误

问题描述

我无法获取 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:“一些描述”
               |_ lect:“一些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)

标签: node.jsfirebasefirebase-realtime-databasegoogle-cloud-functions

解决方案


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

推荐阅读