gpt4 book ai didi

javascript - 引用错误: value is not defined CloudFunction

转载 作者:行者123 更新时间:2023-12-02 21:07:01 25 4
gpt4 key购买 nike

我正在尝试向设备在 firebase 数据库中拥有的所有用户发送通知,但它在云函数的寄存器中显示:“ReferenceError:值未定义”。

应该做的是:每次在“通知”中进行更改时,您必须获取更改的数据,并使用此数据在设备表中查找并向该中的所有 token 发送通知设备。

这是错误:

Error of Firebase

云函数代码:

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
})

数据库:

Firebase Database

最佳答案

在这一行中:

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

value 是一个您从未定义过的变量。这就是错误消息的含义。您的意思可能是这样,将 'value' 作为字符串:

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

关于javascript - 引用错误: value is not defined CloudFunction,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61199361/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com