0

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

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Okay, it solve this error and give me another. As You told, i must to change some commands. However thank You for reply. It's really helpful
Thank's for this sample. Something strange. I've clicked yet, but I haven't enought reputation points to display this operation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.