node.js - 尝试获取参数时出现 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)
解决方案
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]);
});
推荐阅读
- python - Python concurrent.futures.ThreadPoolExecutor max_workers
- c++ - *min-heap* 的良好 C++ 数据结构实现,增加 *hash table*
- vhdl - 在 VHDL 中为模块创建“初始化”和“结束”信号
- ruby - 如何正确使用 Google::Apis::SheetsV4::BatchUpdateSpreadsheetRequest 对象?
- javascript - 我需要有关如何从节点 js 在 sql server 中插入的帮助
- python - 将控制权交还给运输工具
- metal - Metal Compute 与 ARM Neon
- c++ - 在没有活动异常的情况下调用终止(抛出 catch all 表达式)
- r - R中逻辑回归的模型框架默认错误变量长度不同
- python-3.x - 这两个python3版本有什么区别?