最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ReferenceError: context is not defined - Stack Overflow

programmeradmin2浏览0评论

I am trying to create a sendNotification function by using node.js and firebase function but unfortunately I am currently stuck. I never got a notification from the sender and when I look to Firebase function logs, I saw an error says

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)

this is my code index.js code

`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');

    });

});

});

I don't know where is the error here can someone tell me where is the error and how can I resolve it?

I am trying to create a sendNotification function by using node.js and firebase function but unfortunately I am currently stuck. I never got a notification from the sender and when I look to Firebase function logs, I saw an error says

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)

this is my code index.js code

`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');

    });

});

});

I don't know where is the error here can someone tell me where is the error and how can I resolve it?

Share Improve this question edited Apr 17, 2018 at 10:08 Peter Haddad 80.9k25 gold badges145 silver badges146 bronze badges asked Apr 17, 2018 at 5:21 user8724610user8724610 3
  • 1 It's exactly as the error says - you never define context before you use it...? – CertainPerformance Commented Apr 17, 2018 at 5:22
  • Okay, so how can I define the context here? – user8724610 Commented Apr 17, 2018 at 5:24
  • I don't know, it's your app - you seem to have something in mind when you access its .params.user_id. Figure out what that object is, and assign it to context before you use it – CertainPerformance Commented Apr 17, 2018 at 5:26
Add a ment  | 

2 Answers 2

Reset to default 2

Change it to this:

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

const user_id = context.params.user_id;

});

onWrite has two parameters change and context. You use context to access the wildcards example user_id.

more info here:

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

I managed to solved my own problem by the help of @PeterHaddad. I read the link that he post and I change some of my code. Here it is

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');

    });

});

});
发布评论

评论列表(0)

  1. 暂无评论