Javascript 通知功能但没有输出文本

Javascript Notification function but no output text

我为我的 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
}

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

这是代码的 Pastebin link:https://pastebin.com/n0TaXxcJ

// Combine texts.
let outputText = userText + ' ' + actionText
if (parentText) {
  outputText = outputText + parentText
}

return outputText

你最终覆盖了outputText。另外,您将其定义为另一个变量,请改用 let 。

此外,最好使用 config 之类的包,而不是 copy/pasting const DRIVE = 'drv' 和其他包。如果您决定更改其他内容,则必须在任何地方进行更改。


编辑:

你可以替换那个

// Combine texts.
const outputText = userText + ' ' + actionText
if (parentText) {
  const outputText = parentText
}
// Return output text.
return outputText

块与return userText + ' ' + actionText + ' ' + parentText并获得相同的效果。如果 parentText 为空,则不会添加任何内容,对吗?