简体   繁体   English

无法读取未定义的属性“包含” - 快速路由

[英]Cannot read property 'include' of undefined - Express Routing

I'm trying to do some basic routing depending on what is ticked/checked on the first (parent) page.我正在尝试根据第一(父)页面上勾选/检查的内容进行一些基本路由。 So if the first page had 3 options and the user selects 2 of them then I'd expect the user to see the two (child) pages selected but not the third (as it was not selected).因此,如果第一页有 3 个选项并且用户选择了其中的 2 个,那么我希望用户看到选择的两个(子)页面,而不是第三个(因为它没有被选中)。

My approach was on the first (parent) page to do the following:我的方法是在第一个(父)页面上执行以下操作:

router.post('/first-page/', function (req, res, next) {
  var errors = []
  var myCheckboxes = req.body.checkboxes
  if (myCheckboxes === 'undefined') {
   //error validation
  } if (myCheckboxes.includes('first')) {
    res.redirect('first-option-page')
    return
  } if (myCheckboxes.includes('second')) {
    res.redirect('second-option-page')
    return
  } if (myCheckboxes.includes('third')) {
    res.redirect('third-option-page')
    return
  } else {
    res.redirect('move-on')
    return
  }
}) 

Then for the other pages what I was doing is used the req.session.data['checkboxes'] and run through the loop again to see if the second and/or the third option page is selected and if they are then show those pages until eventually it is has gone through it all.然后对于其他页面,我正在使用req.session.data['checkboxes']并再次运行循环以查看是否选择了第二个和/或第三个选项页面,然后显示这些页面直到最终它经历了这一切。

The problem I got is that it is then throwing the error "Cannot read property 'include' of undefined" for the child pages.我遇到的问题是它会为子页面抛出错误“无法读取未定义的属性'include'”。

router.post('/first-option-page/', function (req, res, next) {
  var errors = []
  var myCheckboxes = req.session.checkboxes
  if (myCheckboxes.includes('second')) {
    res.redirect('second-option-page')
    return
  } if (myCheckboxes.includes('third')) {
    res.redirect('third-option-page')
    return
  } else {
    res.redirect('move-on')
    return
  }
}) 

Is there a slicker way to run through the pages or what can I do to solve the error please?有没有更流畅的方式来浏览页面,或者我能做些什么来解决这个错误?

Found my problem, I wrote the req.session.checkboxes incorrectly it should have been req.session.data['checkboxes']发现我的问题,我错误地写了req.session.checkboxes它应该是req.session.data['checkboxes']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM