gpt4 book ai didi

javascript - 错误 : Registration token(s) provided to sendToDevice() must be a non-empty string or a non-empty array

转载 作者:行者123 更新时间:2023-11-30 08:22:37 28 4
gpt4 key购买 nike

我正在尝试使用 firebase 云功能将通知发送到一个点半径的圆内的设备。我能够获取圈内设备的 ID,但无法获取 token ,使用 console.log(token) 打印时 token 为空。

     const getdevicetokenpromise = db.ref('/DriversAvailable/{key}/token').once('value');
console.log(key); //this value is right

return getdevicetokenpromise.then(result => {
console.log(result.val()); //this value is null, this is the problem
var token = result.val();
const payload = {
notification: {
title: 'title',
body: 'hey, well done dude',
icon: 'default'

}
};



return admin.messaging().sendToDevice(token, payload)
.then((response)=> {
return console.log("Successfully sent message:", response);
})
.catch((error) =>{
console.log("Error sending message:", error);
});

});

我已经尝试了 stackoverflow 上建议的大部分内容,但找不到解决方案。谢谢。

最佳答案

看起来你假设 key 的值应该被插入到这个字符串中:

 const getdevicetokenpromise = db.ref('/DriversAvailable/{key}/token').once('value');

不过,这不是它的工作方式。您实际上是在查询那个确切的字符串,而没有插入 key。我想你打算使用 JavaScript 语法进行变量插值,使用字符串周围的反引号和 ${} 来分隔变量:

 const getdevicetokenpromise = db.ref(`/DriversAvailable/${key}/token`).once('value');

关于javascript - 错误 : Registration token(s) provided to sendToDevice() must be a non-empty string or a non-empty array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51090238/

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