TypeError: Невозможно прочитать свойство 'receive_id' из неопределенного - PullRequest
0 голосов
/ 06 июля 2018

Эта функция предназначена для уведомления пользователя, когда кто-то отправляет ему запрос. Я создаю приложение обмена сообщениями для учебных целей и не могу решить эту проблему. консоль firebase продолжает выдавать ошибку: firebase functions

TypeError: Cannot read property 'receiver_id' of undefined
   at exports.sendNotification.functions.database.ref.onWrite.event 
(/user_code/index.js:9:36)
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:728:24
    at process._tickDomainCallback (internal/process/next_tick.js:135:7)

----- Мой код -----

'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;
        console.log('We ahve a notification to send to : ', receiver_id);
        if(!event.data.val())
        {return console.log('Notification has been deleted', 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 recived a new friend request",
                icon : "default"
            }
        };
        return admin.messaging().sendToDevice(token_id,payload)
            .then(response =>
            {
                return console.log('This is notification');
            });
        });
    });

1 Ответ

0 голосов
/ 06 июля 2018

params предоставляются 2-м параметром функции обратного вызова onWrite.

.onWrite((snapshot, context) => {
  const receiver_id = context.params.receiver_id;
});
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...