gpt4 book ai didi

javascript - Firebase JS 函数似乎向变量添加前导空格

转载 作者:行者123 更新时间:2023-12-02 20:50:58 24 4
gpt4 key购买 nike

第一篇文章及其愚蠢之处让我不寒而栗。

使用 Firebase 在 Android 中开发一款 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)

我将传入消息记录为 (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 个)。

关于javascript - Firebase JS 函数似乎向变量添加前导空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61604874/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com