Javascript通知功能,但无输出文本

时间:2018-11-17 11:01:45

标签: javascript if-statement multiple-conditions

我为Vue应用程序编写了一个通知功能,然后将其导入商店(Vuex)。

export default function getNotificationText(notification) {
  // Parent types.
  const DRIVE = 'drv'
  const DRIVE_GALLERY = 'dgl'
  const EXPERIENCE = 'exp'
  const EXPERIENCE_GALLERY = 'egl'
  const STAY = 'sty'
  const STAY_GALLERY = 'sgl'
  const LOCATION_PHOTO = 'lpt'
  const DRIVE_PHOTO = 'dpt'
  const EXPERIENCE_PHOTO = 'ept'
  const STAY_PHOTO = 'spt'
  const LOCATION_REVIEW = 'lrv'
  const DRIVE_REVIEW = 'drv'
  const EXPERIENCE_REVIEW = 'erv'
  const STAY_REVIEW = 'srv'
  const LOCATION_QUESTION = 'lqt'
  const DRIVE_QUESTION = 'dqt'
  const EXPERIENCE_QUESTION = 'eqt'
  const STAY_QUESTION = 'sqt'
  const TRIP = 'trp'
  const TRIP_PHOTO = 'tpt'
  const DRIVE_GALLERY_COMMENT = 'dgc'
  const EXPERIENCE_GALLERY_COMMENT = 'egc'
  const STAY_GALLERY_COMMENT = 'sgc'
  const LOCATION_PHOTO_COMMENT = 'lpc'
  const DRIVE_PHOTO_COMMENT = 'dpc'
  const EXPERIENCE_PHOTO_COMMENT = 'epc'
  const STAY_PHOTO_COMMENT = 'spc'
  const LOCATION_REVIEW_COMMENT = 'lrc'
  const DRIVE_REVIEW_COMMENT = 'drc'
  const EXPERIENCE_REVIEW_COMMENT = 'erc'
  const STAY_REVIEW_COMMENT = 'src'
  const LOCATION_QUESTION_COMMENT = 'lqc'
  const DRIVE_QUESTION_COMMENT = 'dqc'
  const EXPERIENCE_QUESTION_COMMENT = 'eqc'
  const STAY_QUESTION_COMMENT = 'sqc'
  const TRIP_COMMENT = 'trc'
  const TRIP_PHOTO_COMMENT = 'tpc'
  const USER = 'usr'
  const notificationText = ''
  // Build user text.
  const userText = notification.sender.first_name + ' ' + notification.sender.last_name
  // Build action text.
  const action = notification.action
  const actionText = ''
  // actions started
  const COMMENT = 'cmt'
  const LOVE = 'lov'
  const FOLLOW = 'flw'
  const ACCEPT_BOOKING = 'apb'
  const ADD_PHOTO = 'adp'
  const ADD_REVIEW = 'adr'
  const ADD_QUESTION = 'adq'
  if (action == COMMENT) {
    const actionText = 'commented on your'
  } else if (action === LOVE) {
    const actionText = 'liked your'
  } else if (action === FOLLOW) {
    const actionText = 'started following you'
  } else if (action === ACCEPT_BOOKING) {
    const actionText = 'accepted your booking'
  } else if (action === ADD_PHOTO) {
    const actionText = 'added a new photo on your'
  } else if (action === ADD_REVIEW) {
    const actionText = 'added a new review on your '
  } else if (action === ADD_QUESTION) {
    const actionText = 'asked a new question on your'
  }
  // Build parent text.
  const parentType = notification.parent.type
  const parentText = ''
  if (action === ADD_PHOTO || action === ADD_REVIEW || action === ADD_QUESTION) {
    if (parentType === DRIVE_PHOTO || parentType === DRIVE_REVIEW || parentType === DRIVE_QUESTION) {
      const parentText = 'drive'
    } else if (parentType === EXPERIENCE_PHOTO || parentType === EXPERIENCE_REVIEW || parentType === EXPERIENCE_QUESTION) {
      const parentText = 'experience'
    } else if (parentType === STAY_PHOTO || parentType === STAY_REVIEW || parentType === STAY_QUESTION) {
      const parentText = 'stay'
    }
  } else if (action === COMMENT || action === LOVE) {
    if (parentType === DRIVE_GALLERY || parentType === EXPERIENCE_GALLERY || parentType === STAY_GALLERY || parentType === LOCATION_PHOTO || parentType === DRIVE_PHOTO || parentType === EXPERIENCE_PHOTO || parentType === STAY_PHOTO || parentType === TRIP_PHOTO) {
      const parentText = 'photo'
    } else if (parentType === LOCATION_REVIEW || parentType === DRIVE_REVIEW || parentType === EXPERIENCE_REVIEW || parentType === STAY_REVIEW) {
      const parentText = 'review'
    } else if (parentType === LOCATION_QUESTION || parentType === DRIVE_QUESTION || parentType === EXPERIENCE_QUESTION || parentType === STAY_QUESTION) {
      const parentText = 'question'
    } else if (parentType === TRIP) {
      const parentText = 'trip'
    } else if (['DRIVE_GALLERY_COMMENT', 'EXPERIENCE_GALLERY_COMMENT', 'STAY_GALLERY_COMMENT', 'LOCATION_PHOTO_COMMENT', 'DRIVE_PHOTO_COMMENT', 'EXPERIENCE_PHOTO_COMMENT', 'STAY_PHOTO_COMMENT', 'LOCATION_REVIEW_COMMENT', 'DRIVE_REVIEW_COMMENT', 'EXPERIENCE_REVIEW_COMMENT', 'STAY_REVIEW_COMMENT', 'LOCATION_QUESTION_COMMENT', 'DRIVE_QUESTION_COMMENT', 'EXPERIENCE_QUESTION_COMMENT', 'STAY_QUESTION_COMMENT', 'TRIP_COMMENT', 'TRIP_PHOTO_COMMENT'].indexOf(parentType) > -1) {
      const parentText = 'comment'
      console.log(parentText)
    }
  }
  // Combine texts.
  const outputText = userText + ' ' + actionText
  if (parentText) {
    const outputText = parentText
  }
  // Return output text.
  return outputText
}

我想根据条件userText已被定义为用户firstnamelastname来显示输出文本,但是我没有获得ActionText或ParentText。 if语句的内部工作正常,我可以得到console.log,但if语句之外的工作不正常。请帮助我。

以下是代码的Pastebin链接:https://pastebin.com/n0TaXxcJ

1 个答案:

答案 0 :(得分:0)

>>> ''.join(dict.fromkeys('BANANARAMA'))
'BANRM'

您最终将覆盖// Combine texts. let outputText = userText + ' ' + actionText if (parentText) { outputText = outputText + parentText } return outputText 。另外,您将其定义为另一个变量,请使用let代替。

此外,最好使用config之类的软件包,而不是在各处复制/粘贴outputText及其他软件包。如果您决定进行其他更改,则必须在任何地方进行更改。


编辑:

您可以替换

const DRIVE = 'drv'

// Combine texts. const outputText = userText + ' ' + actionText if (parentText) { const outputText = parentText } // Return output text. return outputText 阻止并获得相同的效果。如果parentText为空,则不会添加任何内容,对吧?