5 javascript node.js firebase firebase-realtime-database google-cloud-functions
我正在尝试使用 node.js 和 firebase 函数创建 sendNotification 函数,但不幸的是我目前陷入困境。我从未收到发件人的通知,当我查看 Firebase 函数日志时,我看到一条错误消息
ReferenceError: context is not defined
at exports.sendNotification.functions.database.ref.onWrite.event (/user_code/index.js:9:32)
at Object.<anonymous> (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:112:27)
at next (native)
at /user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71
at __awaiter (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:24:12)
at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:82:36)
at /var/tmp/worker/worker.js:700:26
at process._tickDomainCallback (internal/process/next_tick.js:135:7)
Run Code Online (Sandbox Code Playgroud)
这是我的代码index.js代码
`const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNotification = functions.database.ref('notifications/{user_id}').onWrite(event => {
const user_id = context.params.user_id;
const notification_id = context.params.notification_id;
console.log('We have notification from: ', user_id);
if (!event.val()) {
return console.log('A Notification has been deleted from the database: ', notification_id);
}
const fromUser = admin.database().ref(`/Users/${user_id}/device_token`).once('value');
return fromUser.then(result => {
const token_id = result.val();
const payload = {
notification: {
title: "Friend Request",
body: "You've received a Friend Request",
icon: "default"
}
};
return admin.messaging().sendToDevice(token_id , payload).then(response => {
console.log('This was the notification feature');
});
});
});
Run Code Online (Sandbox Code Playgroud)
我不知道错误在哪里,有人可以告诉我错误在哪里以及如何解决吗?
改成这样:
exports.sendNotification = functions.database.ref('notifications/{user_id}').onWrite((change,context) => {
const user_id = context.params.user_id;
});
Run Code Online (Sandbox Code Playgroud)
onWrite
有两个参数change
和context
。您使用context
通配符来访问示例user_id
。
更多信息在这里:
https://firebase.google.com/docs/functions/beta-v1-diff
归档时间: |
|
查看次数: |
15861 次 |
最近记录: |