【发布时间】:2020-12-21 14:41:18
【问题描述】:
我正在尝试使用本教程获取推送通知: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:7)
这里是云功能:
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 而不是 s in this case: admin.database().ref(/fcmTokens/${userId})` 时,他们中的大多数人不得不这样做。如您所见,我使用的是刻度,所以我不确定这里出了什么问题。
显然,我砍掉了 ID,但我只是想说明它直接嵌套在 /fcmTokens 下。
【问题讨论】:
-
您需要显示有关数据库结构的更多详细信息。
/model_outputs/real_estate在哪里?const userId = change.after.val(); console.log(userId);有什么用? -
@RenaudTarnec
/model_outputs/在我的数据库的顶层。所以,基本上,我希望在/real_estate下的任何内容被写入时发生通知。我在console.log中添加了userID,出现了一个错误,说是未定义。
标签: node.js firebase firebase-realtime-database google-cloud-functions