Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript ReferenceError:函数中未定义值_Javascript_Google Cloud Functions_Firebase Cloud Messaging_Firebase Notifications - Fatal编程技术网

Javascript ReferenceError:函数中未定义值

Javascript ReferenceError:函数中未定义值,javascript,google-cloud-functions,firebase-cloud-messaging,firebase-notifications,Javascript,Google Cloud Functions,Firebase Cloud Messaging,Firebase Notifications,我试图向firebase数据库中的所有用户发送一个通知,但它说:ReferenceError:云函数的寄存器中没有定义值 应该做的是:每次在通知中进行更改时,您都必须获取更改的数据,使用该数据查看设备表,并向该设备中的所有令牌发送通知 这就是错误: cloudfunction的代码: const functions = require('firebase-functions'); const admin = require('firebase-admin'); // this is the A

我试图向firebase数据库中的所有用户发送一个通知,但它说:ReferenceError:云函数的寄存器中没有定义值

应该做的是:每次在通知中进行更改时,您都必须获取更改的数据,使用该数据查看设备表,并向该设备中的所有令牌发送通知

这就是错误:

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
})
数据库:

在这方面:

const usersSnap = await db.ref('Devices/'+ dev + '/Users').once(value)
值是一个从未定义过的变量。这就是错误消息的意思。您可能是指“value”作为字符串:

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

谢谢它可以纠正错误消息,但仍然不起作用,你知道为什么吗?如果你有新问题,请花一些时间调试它,如果你不能解决它,请发布一个新的问题和新的细节。