javascript - Cloud Functions 无法从实时数据库中获取令牌
问题描述
我试图从函数发送消息,但它总是得到“提供给 sendToDevice() 的注册令牌必须是非空字符串或非空数组”,但这是我的令牌存储的地方:
小路
-- UID
+++-- 令牌
这里是我的代码
exports.mess = functions.database.ref('/path/{uid}')
.onWrite(event => {
var ref = admin.database().ref(`token`);
return ref.once("value", function(snapshot){
const payload = {
notification: {
title: 'You have been invited to a trip.',
body: 'Tap here to check it out!'
}
};
admin.messaging().sendToDevice(snapshot.val(), payload)
},function (errorObject) {
console.log("The read failed: " + errorObject.code);
});
});
解决方案
我认为您正在尝试使用token
触发云功能的用户的属性值。在这种情况下,您不需要附加单独的侦听器,因为该值已经在context
参数中可用。
exports.mess = functions.database.ref('/path/{uid}')
.onWrite(event => {
let token = event.after.child('token').val()
const payload = {
notification: {
title: 'You have been invited to a trip.',
body: 'Tap here to check it out!'
}
};
return admin.messaging().sendToDevice(snapshot.val(), payload)
});
推荐阅读
- python - 如何在 python 中正确预测近期的值?
- python - 如何将余数添加到现有整数?
- arrays - 当我在主函数之外初始化结构成员时,为什么这个 c 程序会出错?
- java - 从嵌套数组中打印/查找元素
- ms-project - Project Server 使用 ReST API 移动/重新排序任务
- reactjs - TypeError:无法读取 loadImage 处未定义的属性“url”
- python - 我如何在 python 中将 math.cos 函数用于 list 或 arange
- typescript - Typescript 如何选择和组合嵌套类型作为根类型
- reactjs - 在 React-Beautiful-DnD 中拖放树项目
- node.js - 如何在nest.js 中定义辅助函数和常量?