Javascript Firebase JS函数似乎在变量中添加前导空格

Javascript Firebase JS函数似乎在变量中添加前导空格,javascript,firebase,google-cloud-functions,Javascript,Firebase,Google Cloud Functions,第一篇帖子和它的愚蠢让我不寒而栗 使用Firebase在android上开发play-around messenger应用程序。我有一个Firebase函数,我正试图通过它向接收者发送通知。尝试指定数据库引用时遇到问题 sendNewMessageNotification Error: Reference.child failed: First argument was an invalid path = "/Users/${senderUid}". Paths must be non-emp

第一篇帖子和它的愚蠢让我不寒而栗

使用Firebase在android上开发play-around messenger应用程序。我有一个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)
但是当我记录属性转换为什么时,我看到有一个前导空格(第17行):

函数的完整代码:

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)。

看来我认为这是一个愚蠢的问题,我是对的。谢谢道格的回答,问题马上解决了。干杯
'/Users/${senderUid}'
`/Users/${senderUid}`