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

Я пытаюсь закодировать функцию, которая срабатывает, когда пользователь получает новый комментарий и отправляет уведомление. Комментарии хранятся в / Users / {user_id} / Notifications / {Notification_id}.Пользователи сохраняют свои токены уведомлений устройств в / users / {userID}

    'use strict'

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

exports.sendNotif = functions.firestore.document('Users/{user_id}/Notifications/{notification_id}')
.onWrite((change,context) =>
{
    const user_id = context.params.user_id;
    const notification_id = context.params.notification_id;

    return admin.firestore().collection('Users').document(user_id).collection('Notifications').document(notification_id).get().then(queryResult=>{

      const from_user_id = queryResult.data().From;

      const from_data = admin.firestore().collection('Users').document(from_user_id).get();
      const to_data = admin.firestore().collection('Users').document(user_id).get();

      return Promise.all([from_data, to_data]).then(result => {

        const from_name = result[0].data().From;
        const to_name = result[1].data().From;

        console.log("FROM: " + from_name + "TO: " + to_name);

      });

    });

});

А вот и файл package.json.Все обновлено

 {
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "serve": "firebase serve --only functions",
    "shell": "firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "dependencies": {
    "firebase-admin": "^6.4.0",
    "firebase-functions": "^2.1.0"
  },
  "private": true
}


    {
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "serve": "firebase serve --only functions",
    "shell": "firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "dependencies": {
    "firebase-admin": "^6.4.0",
    "firebase-functions": "^2.1.0"
  },
  "private": true
}

Firebase выдает следующую ошибку:

TypeError: Cannot read property 'from' of undefined
at Promise.all.then.result (/user_code/index.js:21:47)
at process._tickDomainCallback (internal/process/next_tick.js:135:7)

1 Ответ

0 голосов
/ 01 января 2019

Пожалуйста, проверьте длину вашего массива (результат).Вы должны зарегистрировать результат.

 return Promise.all([from_data, to_data]).then(result => {
    console.log(result);

    const from_name = result[0].data().From;
    const to_name = result[1].data().From;

    console.log("FROM: " + from_name + "TO: " + to_name);

  });
...