Apha25
Apha25

Reputation: 27

ReferenceError: value is not defined CloudFunction

ïm trying to send a notifications to all the users that a device have in firebase database, but it says: "ReferenceError: value is not defined" in the register of the cloud function.

What is supposed to do is: every time a change is made in "Notifications", you must obtain the data that was changed, with this data look in the device table and send a notification to all the tokens that are in that device.

This is the error:

Error of Firebase

Code of the cloudfunction:

const functions = require('firebase-functions');
const admin = require('firebase-admin');  // this is the Admin SDK, not firebase-functions

admin.initializeApp(functions.config().firebase);

const db = admin.database()
const ms = admin.messaging()

exports.notifications = functions.database.ref('Notifications/{id}').onUpdate(async(evt) =>{

const payload = {
    notification:{
        title   : 'Alarma se activó tu cerca',
        body    : 'Se activó tu cerca, revisala',
        badge   : '1',
        sound   :'defaul'   
    }
};

//Get Notification Device ID
const notySnap = await db.ref('Notification/').once('value');

var devicee = notySnap.notty;
var dev = JSON.stringify(devicee);

//Get number of users that the device had
const usersSnap = await db.ref('Devices/'+ dev + '/Users').once(value)
const nUsers = usersSnap.nUsers;
var Nusers = JSON.stringify(nUsers);
var nNUsers = parseInt(Nusers);

//Send notification to the number of users that exist
if (Nusers !== null){
    for(i = 1; 1 <=nNUsers; i++){
        if(i === 1){
            const userToSendP1 = usersSnap.user1;
            var userToSend1 = JSON.stringify(userToSendP1);
            Console.log("Mensaje enviado a user 1");
            return ms.sendToDevice(userToSend1, payload);
        }else if(i === 2){
            const userToSendP2 = usersSnap.user2;
            var userToSend2 = JSON.stringify(userToSendP2);
            Console.log("Mensaje enviado a user 2");
            return ms.sendToDevice(userToSend2, payload);
        }else if(i === 2){
            const userToSendP3 = usersSnap.user3;
            var userToSend3 = JSON.stringify(userToSendP3);
            Console.log("Mensaje enviado a user 3");
            return ms.sendToDevice(userToSend3, payload);
        }else if(i === 2){
            const userToSendP4 = usersSnap.user4;
            var userToSend4 = JSON.stringify(userToSendP4);
            Console.log("Mensaje enviado a user 4");
            return ms.sendToDevice(userToSend4, payload);
        }
    }
}
return null
})

DataBase:

Firebase Database

Upvotes: 0

Views: 254

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317692

In this line:

const usersSnap = await db.ref('Devices/'+ dev + '/Users').once(value)

value is a variable that you never defined. That's what the error message means. You probably meant this instead, with 'value' as a string:

const usersSnap = await db.ref('Devices/'+ dev + '/Users').once('value')

Upvotes: 1

Related Questions