user8724610
user8724610

Reputation:

ReferenceError: context is not defined

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?

Upvotes: 5

Views: 17357

Answers (2)

user8724610
user8724610

Reputation:

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

    });

});

});

Upvotes: 0

Peter Haddad
Peter Haddad

Reputation: 80914

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.com/docs/functions/beta-v1-diff

Upvotes: 2

Related Questions