node.js - Firebase 云函数 - 错误:Reference.child 失败:第一个参数是无效路径
问题描述
我正在尝试使用本教程获取推送通知:https ://www.youtube.com/watch?v=z27IroVNFLI
它显然有点老了,但 Firebase 网络应用程序没有很多好的替代品。
我的云功能运行时出现以下错误:
fcm发送
错误:Reference.child 失败:第一个参数是无效路径 =“/fcmTokens/[object Object]”。路径必须是非空字符串,并且在 validatePathString (/srv/node_modules/@firebase/database/dist/index.node) 中不能包含“.”、“#”、“$”、“[”或“]” .cjs.js:1636:15) 在 validateRootPathString (/srv/node_modules/@firebase/database/dist/index.node.cjs.js:1647:5) 在 Reference.child (/srv/node_modules/@firebase/database /dist/index.node.cjs.js:13688:17) 在 Database.ref (/srv/node_modules/@firebase/database/dist/index.node.cjs.js:14862:48) 在 exports.fcmSend.functions .database.ref.onWrite (/srv/index.js:21:12) 在 cloudFunction (/srv/node_modules/firebase-functions/lib/cloud-functions.js:131:23) 在 /worker/worker.js: 825:24 at process._tickDomainCallback (internal/process/next_tick.js:229:
这是云功能:
exports.fcmSend = functions.database
.ref('/model_outputs/real_estate')
.onWrite((change, context) => {
const userId = change.after.val();
console.log(change);
const payload = {
notification: {
title: "test",
body: "test body",
icon: "https://placeimg.com/250/250/people"
}
};
admin.database()
.ref(`/fcmTokens/${userId}`)
.once('value')
.then(token => token.val() )
.then(userFcmToken => {
return admin.messaging().sendToDevice(userFcmToken, payload)
})
.then(res => {
console.log("Sent Successfully", res);
return null;
})
.catch(err => {
console.log(err);
});
});
s in this case:
我已经看到其他人发布了有关此错误的帖子,但是当他们使用 's 而不是admin.database().ref( /fcmTokens/${userId}
)`时,他们中的大多数人不得不这样做。如您所见,我正在使用刻度,所以我不确定这里出了什么问题。
显然,我砍掉了 ID,但我只是想说明它直接嵌套在/fcmTokens
.
解决方案
您的 Cloud Function 似乎有几个问题:
#1
以下行生成错误,因为userId
它不是字符串。
admin.database().ref(`/fcmTokens/${userId}`)
DB节点的值很可能'/model_outputs/real_estate'
是一个对象,因此当您const userId = change.after.val();
将对象分配给userId
在您上面的评论之后更新:看来您得到undefined
了userId
. 您需要调试并解决这个问题:它必须是一个字符串。
#2
以下承诺链是错误的:
admin.database()
.ref(`/fcmTokens/${userId}`)
.once('value')
.then(token => token.val() ) // You don't return anything here, and not a Promise
.then(userFcmToken => {
return admin.messaging().sendToDevice(userFcmToken, payload)
})
.then(res => {
console.log("Sent Successfully", res);
return null;
})
.catch(err => {
console.log(err);
});
如果我正确理解您的逻辑和数据模型,它应该是以下几行:
admin.database()
.ref(`/fcmTokens/${userId}`)
.once('value')
.then(snapshot => {
const userFcmToken = snapshot.val();
return admin.messaging().sendToDevice(userFcmToken, payload)
})
.then(res => {
console.log("Sent Successfully", res);
return null;
})
.catch(err => {
console.log(err);
});
推荐阅读
- javascript - 指纹js2未捕获的ReferenceError:杂音未定义在
- apache-spark - 如何在 Apache Spark/Hive 中合并 DataFrames 然后增加版本
- sql-server - 从我的 select 语句中捕获任何包含多个 NOT EXISTS 的错误
- docker - 自定义 Hasura Docker 镜像
- angular8 - 如何修复 url 重定向中的默认语言
- excel - 从 excel 粘贴到 powerpoint 时 VBA 中的运行时错误
- google-apps-script - Google Sheets - 使应用程序脚本在手机应用程序版本上运行
- powershell - 不带引号的 PowerShell 哈希表到 JSON
- php - PDO 版本属性从何而来?
- reactjs - 我应该在 React Hook 中为 CRUD 使用操作还是创建可重用函数