Функции Firebase - ReferenceError: событие не определено - PullRequest
0 голосов
/ 24 ноября 2018

Я пытаюсь сделать простую функцию уведомления, но каждый раз, когда я запускаю функцию, я получаю ОШИБКУ

TypeError: Невозможно прочитать 'params' свойства undefined в exports.sendNotification.functions.database..ref.onWrite (/user_code/index.js:23:32) в cloudFunctionNewSignature (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:105:23) в cloudFunction (/ user_code / node_modules / firebase-functions / lib / cloud-functions.js: 135: 20) в /var/tmp/worker/worker.js:758:24 at process._tickDomainCallback (внутренняя / process / next_tick.js: 135: 7)

Или я получаю ошибку

TypeError: Невозможно прочитать свойство 'user_id' из неопределенного

Я получил ответ от здесь

но я ничем не помогал, и я не могу найти проблему.это мой код

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

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

const user_id = event.params.user_id;
const notification_id = event.params.notification_id;

console.log('We have a notification from : ', user_id);

if(!context.data.val()){

return console.log('A Notification has been deleted from the database : ', 
notification_id);

}

const fromUser =admin.database().ref(`/notifications/${user_id}/${notification_id}`).once('value');

return fromUser.then(fromUserResult => {

const from_user_id = fromUserResult.val().from;

console.log('You have new notification from  : ', from_user_id);

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

return Promise.all([userQuery, deviceToken]).then(result => {

  const userName = result[0].val();
  const token_id = result[1].val();

  const payload = {
    notification: {
      title : "New Friend Request",
      body: `${userName} has sent you request`,
      icon: "default",
      click_action : "none"
    },
    data : {
      from_user_id : from_user_id
    }
  };

  return admin.messaging().sendToDevice(token_id, payload).then(response => {

    return console.log('This was the notification Feature');

     });

   });

  });

});

1 Ответ

0 голосов
/ 24 ноября 2018

Вы никогда не определяли event, поэтому он не определен.Вы пытаетесь получить доступ к свойству неопределенной переменной.Вы имели в виду context.params.user_id?

(Возможно, вы используете устаревшее учебное пособие. Я постоянно вижу в «Переполнении стека» плохие функции «sendNotification». Сообщите автору.)

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...