首页 > 解决方案 > Json中未定义的值数组

问题描述

这是我获取登录用户数据的代码

router.post('/login', function (req, res, next) {
  /* look at the 2nd parameter to the below call */
  let errors = [];
  if (!req.body.password || !req.body.email) {
    req.flash('error_msg', 'please enter email and password');
    res.redirect('/login')
  }
  passport.authenticate('local', function (err, user, info) {
    if (err) { return next(err); }
    if (!user) {
      req.flash('error_msg', 'your email or password is not correct');
      res.redirect('/login')
    }
    req.logIn(user, function (err) {
      if (err) { return next(err); }
      var type = user.type;
      var dt = dateTime.create();
      var formatted = dt.format('Y-m-d H:M:S');
      connection.query("update table set last_login = '"+formatted+"' where id = '"+user.id+"'", function (err, rows) {
        if(type == '0'){
          res.redirect('/login');
        }
        else if(type == '1'){
          console.log(user);
          res.cookie('user', user).redirect('/dashboard');
        }
        else{
          res.redirect('/customers');
        }
      });
    });
  })(req, res, next);
});

我正在尝试控制台节点 js 中的一些数据,并且其中的数据正在得到类似的东西。

{ 
  email: 'test@gmail.com',
  username: 'tester',
  id: 9,
  password: '7ZaOhA0Q0tMTqHC8ExpOLDEdetCb3zKzQYFHIh9RpuI=',
  type: '1',
  token: 
 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjp7ImVtYWlsIjoic2l2YWFAZ21haWwuY29tIiwidXNlcm5hbWUiOiJzaXZhYSIsImlkIjo5LCJwYXNzd29yZCI6IjdaYU9oQTBRMHRNVHFIQzhFeHBPTERFZGV0Q2Izekt6UVlGSEloOVJwdUk9IiwidHlwZSI6IjEiLCJ0b2tlbiI6IiJ9LCJpYXQiOjE1MzQ1Njg3NjEsImV4cCI6MTUzNDU3MTc2MX0._3AspvfLO2K46OVilqsLtfiRcZTG2ZYvGJWe6jaQ3ZA',
  customers: undefined 
}

现在我想显示包含 abc、xyz、pqr 之类的数据的客户字段数据,它似乎未定义。那么我如何获取数据而不是未定义,如果您知道,请告诉我未定义值背后的原因

提前感谢您的帮助,让我的一天更美好。

标签: jsonnode.js

解决方案


推荐阅读