简短的if-else返回Javascript错误

时间:2018-12-13 00:35:26

标签: javascript

以下JS代码行无法正常运行。

当imageUrl具有值时,出现以下错误,并且我希望notificationBody为=“您收到了新的图像消息。”

我在这里想念什么?

  

“ notification.body”属性的无效值。值必须是   字符串。

const notificationBody = (imageUrl === "" ? "You received a new image message." : messageTxt)

消息有效载荷:

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

});

2 个答案:

答案 0 :(得分:2)

首先,如果imageUrl不是“”,则notificationBody将是messageTxt的值,而不是“您收到了新的图像消息”。串。

javascript三元语句按以下方式工作:

const foo = (<conditional> ? <value if conditional is True> : <value if conditional is False>)

第二,您确定示例代码与获取错误的代码完全相同吗?在您的示例中,我看不到对notification.body的引用。

答案 1 :(得分:0)

您的三元数未达到您的期望,第一种情况为true,第二种情况为false,因此您可以切换大小写,或者在下面的解决方案中,将比较运算符切换为{{1} }

我怀疑您的代码中正在发生什么,因为您的三进制不正确,是您没有为self.verticalScrollBar.setSizePolicy(sizePolicy) self.verticalScrollBar.setMinimumSize(QtCore.QSize(13, 39)) self.verticalScrollBar.setStatusTip("") self.verticalScrollBar.setSliderPosition(9) self.verticalScrollBar.setOrientation(QtCore.Qt.Vertical) self.verticalScrollBar.setObjectName("verticalScrollBar") 设置任何值,因此它将!==传递到了{{1} }作为messageTxt

的值

如果您还没有这样做,我强烈建议您在undefined周围使用payload

解决方案

body

文档

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator