0
votes

TypeError: Cannot read property 'user_id' of undefined at exports.sendNotification.functions.firestore.document.onWrite.event (/user_code/index.js:9:31) 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:716: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.firestore.document("Users/{user_id}/Notifications/{notification_id}").onWrite(event=>{

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

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


    const from_user_id = queryResult.data().from;
    const from_message =  queryResult.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 = {
            notification:{
              title : "ViewApp",
              body : from_message,
              icon : "default",
              sound : "default",
              click_action :  "guilherme.viewapp.com.viewapp.TARGETNOTIFICATION"
          },
          data : {
            message : from_message,
            from_user_id : from_user_id
          }
    };

    const options = {
       priority: "high",
       timeToLive: 60 * 60 * 24 //24 hours
   };

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

          console.log("Notification Sent.");

      });

    });

  });

});
2
it means event.params is undefinedAndrew Bone

2 Answers

0
votes

It's when you're assigning the value to user_id. The line is const user_id = event.params.user_id; event.params is undefined

0
votes

Change line with problem:

.onWrite(( change,context) => {
 const user_id = context.params.user_id;
 const notification_id = context.params.notification_id;