无法读取未定义的属性“ userID”

时间:2019-04-30 15:30:51

标签: javascript node.js mongodb

我通过注册用户并将其存储到mongoDB中来创建用户。当我使用创建的用户登录时,收到错误消息: 无法读取未定义的属性“ userID”

我不确定为什么无法与创建和存储其会话的用户登录。

router.post('/', urlencodedParser, function(req, res){
  // Check for duplicate/active session
  console.log('req.body');
  console.log(req.body);

  if(req.session.currentUser){
    res.locals.userName = req.session.currentUser[0].firstName;
    UserDB.find({userID: req.session.currentUser.userID})
      .then(function(user){
        ItemDB.find({userID: user[0].userID})
          .then(function(userItemList){
            res.render('myItems', {userItemList: userItemList});
          })
      })

    } else {
      // Error userID undefined
      UserDB.find({userID: req.session.currentUser.userID})
        .then(function(user){
          // Validation check upon sign in
          req.check('username').not().isEmpty().equals(user[0].username);
          req.check('password').not().isEmpty().equals(user[0].password);

          var errors = req.validationErrors();

          if(errors === false){
            // Checks for an active session
            req.session.currentUser = user;
            res.locals.userName = req.session.currentUser[0].firstName;
            currentUser = req.session.currentUser[0];
            ItemDB.find({userID: user.userID})
              .then(function(userItemList){
                res.render('myitems', {userItemList: userItemList});
              })
          } else {
              res.locals.err = true;
              res.render('login');
          }
        });
  }
});

router.get('/', function(req, res){
  // Checks for an active session
  if(req.session.currentUser){
    res.locals.userName = req.session.currentUser[0].firstName;
    UserDB.find({userID: req.session.currentUser.userID})
      .then(function(user){
        ItemDB.find({userID: user[0].userID})
          .then(function(userItemList){
            res.render('myItems', {userItemList: userItemList});
          })
      })

    } else {
      res.locals.err = false;
      res.render('login');
  }
});

0 个答案:

没有答案