Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js Nodejs如何在登录函数中获取用户详细信息_Node.js - Fatal编程技术网

Node.js Nodejs如何在登录函数中获取用户详细信息

Node.js Nodejs如何在登录函数中获取用户详细信息,node.js,Node.js,这是我的user.js / Route for user logins router.post('/authenticate', function(req, res) { User.findOne({ email: req.body.email }).select('email password active').exec(function(err, user) { if (err) throw err; // Throw err if cannot connect //

这是我的user.js

/ Route for user logins
router.post('/authenticate', function(req, res) {
  User.findOne({ email: req.body.email }).select('email password active').exec(function(err, user) {
    if (err) throw err; // Throw err if cannot connect

    // Check if user is found in the database (based on username)           
    if (!user) {
      res.json({ success: false, message: 'Username not found' }); // Username not found in database
    } else if (user) {
      // Check if user does exist, then compare password provided by user
      if (!req.body.password) {
        res.json({ success: false, message: 'No password provided' }); // Password was not provided
      } else {
        var validPassword = user.comparePassword(req.body.password); // Check if password matches password provided by user 
        if (!validPassword) {
          res.json({ success: false, message: 'Could not authenticate password' }); // Password does not match password in database
        } else if (!user.active) {
          res.json({ success: false, message: 'Account is not yet activated. Please check your e-mail for activation link.', expired: true }); // Account is not activated 
        } else {
          //var token = jwt.sign({ email: user.email}, config.secret, { expiresIn: '24h' }); // Logged in: Give user token
          const token = jwt.sign(user.toJSON(), config.secret, {expiresIn: 604800 // 1 week
        });
          res.json({ success: true, message: 'User authenticated!', token: 'JWT '+token,
          user: {
            id: user._id,
            firstname: user.firstname,
            lastname: user.lastname,
            email: user.email
          }
        }); // Return token in JSON object to controller
        }
      }
    }
  });
});

此/身份验证工作正常,但我无法获取用户名和姓氏。我只收到电子邮件,id详细信息,我不知道哪里出错,请帮助我

从此行中的数据库检索用户时

User.findOne({email:req.body.email})。选择('email password active').exec(函数(err,User){

您正在选择仅选择电子邮件密码和活动属性。 如果您想要firstname和lastname,则需要将其更改为

User.findOne({email:req.body.email})。选择('email password active firstname lastname')。exec(函数(err,User){

编辑:更多信息

.select()方法的文档页

声明关于。选择()


指定要包括或排除哪些文档字段(也称为查询“投影”)

我不知道如何从模型中获取用户详细信息。您必须在
中显式传递要检索的列名。选择
子句