nageen nawaz
nageen nawaz

Reputation: 21

Cannot read property 'From' of undefined at /user_code/index.js

I am working on sending notification between two applications. I have tried to do this through node.js but now I am stuck at this error for 1 month but found no solution to it.please help me out in it. this is very important for me.I will be very thankful for any kind of help.

"use-strict"
const functions = require('firebase-functions');
const admin= require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNotification= 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("ServiceProviders").doc(user_id).collection("Notifications").doc("notification_id").get().then(querySnapshot => {
  const from_user_id= querySnapshot.data().From;
  const from_message= querySnapshot.data().message;

  const from_data= admin.firestore.collection("Users").doc(from_user_id).get();
  const to_data= admin.firestore.collection("Users").doc(user_id).get();


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

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

     const payload= {
       notifications:{
         title: "Notification from : " + from_name,
         body: from_message,
         icon:"default"
       }
     };
    return admin.messaging().sendToDevice(token_id, payload).then(result => {
     return console.log("Notification Sent");
    });
  });
});

here is the monstrous error`

Upvotes: 0

Views: 102

Answers (1)

Art
Art

Reputation: 592

Your firestore query isn't returning anything; probably because either the document/collection doesn't exist.

Right off the bat, it stands out that your "notification_id" is queried as string, rather than the const you set earlier. It's kind of a gamble, but changing line 10 to this may help:

return admin.firestore().collection("ServiceProviders").doc(user_id).collection("Notifications").doc(notification_id).get().then(querySnapshot => {

Upvotes: 1

Related Questions