Jacek
Jacek

Reputation: 45

Firebase Notifications using node.js

I'm working with Firebase notifications using node.js. After compile, when I'm sending request to other user of app (request makes notification), firebase log shows error:

TypeError: Cannot read property 'receiver_id' of undefined at exports.sendNotification.functions.database.ref.onWrite.event (/user_code/index.js:12:36) at Object. (/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 code:

'use strict'


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


exports.sendNotification = 
functions.database.ref('/Notifications/{receiver_id}/{notification_id}')
    .onWrite(event => 
    {
        const receiver_id = event.params.receiver_id;

        const notification_id = event.params.notification_id;

        console.log('We have a notification to send to :', receiver_id);

        if(!event.data.val())
        {
            return console.log('A notification has been deleted from the database: ', notification_id);
        }

        const deviceToken = admin.database().ref(`/Users/${receiver_id}/device_token`).once('value');

        return deviceToken.then(result => 
        {
            const token_id = result.val();

            const payload = 
            {
                notification: 
                {
                    title: "Friend Request",
                    body: "you have received a new friend request",
                    icon: "default"
                }
            };

            return admin.messaging().sendToDevice(token_id, payload)
                        .then(response =>
                        {
                            console.log('This was the notification feature.');
                        });
        });
    });

I have read about new APIs on site:

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

I think I must change event to context, but I don't know how. Is anybody know what's the issue? Thank's for any Help :)

Upvotes: 0

Views: 956

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 600071

The Firebase documentation on the new data and context shows where the params now live:

The context parameter provides information about the function's execution. Identical across asynchronous functions types, context contains the fields eventId, timestamp, eventType, resource, and params.

So to get rid of that error, you'll need to change the first bit of your function to:

exports.sendNotification = 
functions.database.ref('/Notifications/{receiver_id}/{notification_id}')
    .onWrite((data, context) => 
    {
        const receiver_id = context.params.receiver_id;

        const notification_id = context.params.notification_id;

        ...

There are more, similar changes that you'll need to make. If you're having a hard time making those yourself, I recommend you check back in with where you got the code from.

Upvotes: 2

Related Questions