1
votes

I'm new at functions in Firebase, had read some changes in the official documentation, but as I'm doing a notification system it throw to me this error when I try to get my token id I think.

My code

     'use strict'

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


/*
 * 'OnWrite' works as 'addValueEventListener' for android. It will fire the function
 * everytime there is some item added, removed or changed from the provided 'database.ref'
 * 'sendNotification' is the name of the function, which can be changed according to
 * your requirement
 */

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


  /*
   * You can store values as variables from the 'database.ref'
   * Just like here, I've done for 'user_id' and 'notification'
   */

  const user_id = context.params.user_id;
  const notification_id = context.params.notification_id;

  console.log('Tenemos una notificacion para mandar a : ', context.params.user_id);

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

    return console.log('Una notificacion se elimino de la base de datos : ', notification_id);

  }

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

  return deviceToken.then(result => {

    const token_id = result.val();

      /*
       * We are creating a 'payload' to create a notification to be sent.
       */

    const payload = {
        notification: {
          title : "Tienes un nuevo seguidor !",
          body: "Tu nuevo seguidor es .... ",
          icon: "default"
        }

      };

       /*
       * Then using admin.messaging() we are sending the payload notification to the token_id of
       * the device we retreived.
       */

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

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

      });

  });



  });

The error

Cannot read property 'val' of undefined at exports.sendNotification.functions.database.ref.onWrite (/user_code/index.js:28:20) 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)

1
Probably context.data is not defined. Have you tried logging it out?Leo Farmer
I think context is working as the return log of the user ID is working fineGastón Saillén
Also that if statement is never executed as I never delete the dataGastón Saillén
I think the problem might be the const token_id , because is the only thing that comes up with Val()Gastón Saillén
@LeoFarmer is correct. context does not have a property named data.Bob Snyder

1 Answers

4
votes

To test if the data has been deleted change your if-statement to:

if (!change.after.val()) {
  return console.log('Una notificacion se elimino de la base de datos : ', notification_id);
}

Described in the documentation.