简写if-else在Javascript中返回错误
下面的JS代码行没有像我预期的那样工作 当imageUrl有一个值时,我得到以下错误,我希望notificationBody为=“您收到了一条新的图像消息。” 我错过了什么 “notification.body”属性的值无效。价值观必须是 字符串 消息有效负载:简写if-else在Javascript中返回错误,javascript,Javascript,下面的JS代码行没有像我预期的那样工作 当imageUrl有一个值时,我得到以下错误,我希望notificationBody为=“您收到了一条新的图像消息。” 我错过了什么 “notification.body”属性的值无效。价值观必须是 字符串 消息有效负载: const payload = { notification: { title: senderName + " sent you a message", b
const payload = {
notification: {
title: senderName + " sent you a message",
body: notificationBody
},
整个功能:
exports.notifyNewMessage = functions.database.ref('/messages/{pushId}').onCreate((snap, context) => {
const messageSnap = snap.val(); //snap.after.val();
const fromId = messageSnap.fromId;
const toId = messageSnap.toId;
const messageTxt = messageSnap.message;
const imageUrl = messageSnap.imageUrl;
console.log('fromId: ', fromId);
console.log('message: ', messageTxt);
// Get the list of device notification tokens.
const getDeviceTokensPromise = admin.database().ref('/fcmtokens/' + toId + '/registrationtokens').once('value');
console.log('getDeviceTokensPromise', getDeviceTokensPromise);
return admin.database().ref('/fcmtokens/' + toId + '/registrationtokens').once('value').then((userTok) => {
const registrationTokens = Object.keys(userTok.val())
console.log('registrationTokens', registrationTokens);
return admin.database().ref('/users/' + fromId).once('value').then((userDoc) => {
const user = userDoc.val(); //snap.after.val();
const senderName = user.firstName //'Vanessa' //userDoc.firstName //get('firstName')
console.log('senderName: ', senderName);
const notificationBody = (imageUrl === "" ? "You received a new image message." : messageTxt)
console.log('imageUrl: ', imageUrl);
console.log('messageTxt: ', messageTxt);
console.log('notificationBody: ', notificationBody);
//build media messages notification
const payload = {
notification: {
title: senderName + " sent you a message",
body: notificationBody
},
data: {
SENDER_NAME: senderName,
SENDER_ID: fromId
}//end data
}//end payload
//send message
return admin.messaging().sendToDevice(registrationTokens, payload).then( response => {
const stillRegisteredTokens = registrationTokens
response.results.forEach((result, index) => {
const error = result.error
if (error) {
const failedRegistrationToken = registrationTokens[index]
console.error('blah', failedRegistrationToken, error)
if (error.code === 'messaging/invalid-registration-token' || error.code === 'messaging/registration-token-not-registered') {
const failedIndex = stillRegisteredTokens.indexOf(failedRegistrationToken)
if (failedIndex > -1) {
stillRegisteredTokens.splice(failedIndex, 1)
}
}
}
})//end forEach
var validTokens = {};
stillRegisteredTokens.forEach(function(element){
console.log('valid token: ', element);
validTokens[element] = true;
});
//updates['registrationtokens'] = stillRegisteredTokens; ....update(updates);
return admin.database().ref('fcmtokens/' + toId + '/registrationtokens').set(validTokens)
// return admin.database().ref("fcmtokens/" + toId).update({
// //registrationTokens: stillRegisteredTokens
// })//end update
})//end sendToDevice
})//end return-then
})//end return-then
});
首先,如果
imageUrl
不是“”,那么notificationBody
将是messageTxt
的值,而不是“youreceiveanewimagemessage.”字符串
javascript三元语句的工作方式如下:
const foo = (<conditional> ? <value if conditional is True> : <value if conditional is False>)
constfoo=(?:)
第二,您确定您的示例代码与出现错误的代码完全相同吗?在您的示例中,我看不到对
notification.body
的引用。首先,如果imageUrl
不是“”,那么notificationBody
将是messageTxt
的值,而不是“您收到了新的图像消息”字符串
javascript三元语句的工作方式如下:
const foo = (<conditional> ? <value if conditional is True> : <value if conditional is False>)
constfoo=(?:)
第二,您确定您的示例代码与出现错误的代码完全相同吗?在您的示例中,我看不到对
通知.body
的引用。您的三元组没有达到预期效果,第一个案例为true,第二个案例为false,因此您可以切换案例,或者在我下面的解决方案中,将比较运算符切换到==代码>
我怀疑您的代码中发生了什么,因为您的三元组不正确,您没有为messageTxt
设置值,因此它将undefined
作为body
的值传递到您的负载中
我还强烈建议您在负载周围使用JSON.stringify()
,如果您还没有这样做的话
解决方案
const senderName='tacocate';
const imageUrl='something';
const messageTxt=4815162342
构造通知体=(
图像URL!==“”?
“您收到了一条新的图像消息。”:
messageTxt
)
const payload=JSON.stringify({
通知:{
标题:senderName+“向您发送消息”,
正文:通知正文
}
})
console.log(payload)
您的三元组没有达到预期效果,第一个案例为true,第二个案例为false,因此您可以切换案例,或者在我下面的解决方案中,将比较运算符切换到==代码>
我怀疑您的代码中发生了什么,因为您的三元组不正确,您没有为messageTxt
设置值,因此它将undefined
作为body
的值传递到您的负载中
我还强烈建议您在负载周围使用JSON.stringify()
,如果您还没有这样做的话
解决方案
const senderName='tacocate';
const imageUrl='something';
const messageTxt=4815162342
构造通知体=(
图像URL!==“”?
“您收到了一条新的图像消息。”:
messageTxt
)
const payload=JSON.stringify({
通知:{
标题:senderName+“向您发送消息”,
正文:通知正文
}
})
console.log(有效负载)
您的设置也是如此notification.body
,正如您的代码所显示的notificationBody
,因此,这看起来不像是错误的来源。我已经更新了我的问题,以包括指定正文的代码,messageTxt的值很可能不是字符串,如果这段代码是导致错误的原因,那么将使用有效负载,这将是抛出错误的原因。它基本上是说,无论您在有效负载.notification.body
中拥有什么值,它都不是一个字符串。是否有可能body:notificationBody
在notificationBody
分配值之前进行计算?您的设置notification.body
,正如您在这里的代码所显示的notificationBody
,因此,这看起来不像是错误的来源。我已经更新了我的问题,以包括指定正文的代码,messageTxt的值很可能不是字符串,如果这段代码是导致错误的原因,那么将使用有效负载,这将是抛出错误的原因。它基本上是说,您在有效负载.notification.body
中拥有的任何值都不是字符串。是否有可能body:notificationBody
在notificationBody
被赋值之前进行计算?