第一篇文章和它的愚蠢让我不寒而栗。
使用Firebase在安卓系统中开发一款即时通讯应用程序。我有一个Firebase函数,我正在尝试通过它向接收器发送通知。尝试指定数据库引用时遇到问题。
sendNewMessageNotification
Error: Reference.child failed: First argument was an invalid path = "/Users/${senderUid}". Paths must be non-empty strings and can't contain ".", "#", "$", "[", or "]"
at validatePathString (/srv/node_modules/@firebase/database/dist/index.node.cjs.js:1667:15)
at validateRootPathString (/srv/node_modules/@firebase/database/dist/index.node.cjs.js:1679:5)
at Reference.child (/srv/node_modules/@firebase/database/dist/index.node.cjs.js:13843:17)
at Database.ref (/srv/node_modules/@firebase/database/dist/index.node.cjs.js:15097:48)
at exports.sendNewMessageNotification.functions.database.ref.onCreate (/srv/index.js:24:41)
at cloudFunction (/srv/node_modules/firebase-functions/lib/cloud-functions.js:131:23)
at /worker/worker.js:825:24
at <anonymous>
at process._tickDomainCallback (internal/process/next_tick.js:229:7)
我将传入消息记录为(ln 13(:
{ blockedByReceiver: false,
concatUserIds: '1CclNhoTiOamWThUTRSQVtNhNpk2Pnwb1pLAh0d1nxpgfdU1HUsYaIA3',
isimage: false,
isseen: false,
message: '66',
receiver: '1CclNhoTiOamWThUTRSQVtNhNpk2',
sender: 'Pnwb1pLAh0d1nxpgfdU1HUsYaIA3',
timesent: 1588645656253,
uid: '-M6XeEOUbGjAU5ut7lQw',
url: '' }
但当我记录属性转换为什么时,我发现有一个前导空格(ln 17(:
sender: Pnwb1pLAh0d1nxpgfdU1HUsYaIA3 receiver: 1CclNhoTiOamWThUTRSQVtNhNpk2 resulting path: /Users/ Pnwb1pLAh0d1nxpgfdU1HUsYaIA3
来自功能的完整代码:
exports.sendNewMessageNotification = functions.database.ref('/Chats/{newChat}')
.onCreate(async (change, context) => {
console.log(change.val());
const message=change.val();
const senderUid=message.sender.trim();
const receiverUid=message.receiver.trim();
console.log('sender:', message.sender, ' receiver:', message.receiver, ' resulting path: /Users/', senderUid);
const sender = admin.database().ref('/Users/${senderUid}').once('value');
const receiver = admin.database().ref('/Users/${receiverUid}').once('value');
const blockList = admin.database.ref('/BlockList').orderByChild( "blockerUid").equalTo(receiverUid).once('value');
const results = await Promise.all([sender, receiver, blockList]);
senderSnapshot = results[0];
receiverSnapshot = results[1];
blockerSnapshot = results[2];
blockerSnapshot.forEach( result => {
if (result.blockeeUid == sender.id) {
console.log("message ${message.id} from ${sender.id} blocked by ${receiver.id}");
return snapshot.ref.child("blockedByReceiver").set(true);
}
} );
const payload = {
notification: {
title: 'You have a new message!',
body: '${sender.username} has send you a message.',
icon: iconUrl
}
}
return admin.messaging().sendToDevice(receiver.token, payload)
.then(function(response) {
console.log("New Message notification for message ${message.id} sent successfully to user ${message.receiver}");
})
.catch(function(error) {
console.log("Error encountered sending new message notification for message ${message.id} sent successfully to user ${message.receiver}, error:", error);
});
});
我尝试了几件不同的事情,整晚都在四处寻找,但都无济于事,任何帮助都将不胜感激。
看起来您希望将变量的值插入字符串中:
'/Users/${senderUid}'
单引号是不可能的。你需要使用回溯报价:
`/Users/${senderUid}`
您应该对所有期望变量插值的字符串进行此更改(根据我的计数,至少为4(。