ReferenceError:未定义上下文

时间:2018-04-17 05:21:12

标签: 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)

这是我的代码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');

    });

});

});

我不知道这里的错误在哪里可以告诉我错误在哪里以及如何解决?

2 个答案:

答案 0 :(得分:1)

将其更改为:

exports.sendNotification = functions.database.ref('notifications/{user_id}').onWrite((change,context) => {

const user_id = context.params.user_id;

});

onWrite有两个参数changecontext。您可以使用context访问通配符示例user_id

更多信息:

https://firebase.google.com/docs/functions/beta-v1-diff

答案 1 :(得分:0)

我设法在@PeterHaddad的帮助下解决了我自己的问题。我阅读了他发布的链接,并更改了我的一些代码。这是

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.sendNotification = functions.database.ref('notifications/{user_id}').onWrite((change, context) => {

const user_id = context.params.user_id;
const notification_id = context.params.notification_id;

console.log('We have notification from: ', user_id);

if (!change.after.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');

    });

});

});