简体   繁体   English

无法使用 JWT 在 Express 上读取未定义的属性“用户名”

[英]Cannot read property 'username' of undefined on Express with JWT

While implementing JWT based login signup on Express js with Mongodb , when I tried to match the user given input to match database username I got this error:使用 MongodbExpress js上实现基于 JWT 的登录注册时,当我尝试匹配用户给定的输入以匹配数据库用户名时,我收到此错误:

TypeError: Cannot read property 'username' of undefined类型错误:无法读取未定义的属性“用户名”

this is my code:这是我的代码:

app.js应用程序.js

const express = require('express');
const path = require('path');
const app = express();
const  jwt  =  require('jsonwebtoken');
const  bcrypt  =  require('bcrypt');
const mongoose = require('mongoose');
const StudentModel = require('./studentmodel');
const SECRET_KEY = "secretkey23456";

const port = process.env.PORT || 3002;
//mongoose coonection
// body Parser Middleware
app.use(express.json());
app.use(express.urlencoded({ extended: false }));

app.post('/register', (req, res) => {

const student = new StudentModel();
let salt = bcrypt.genSaltSync(10);
 student.username  =  req.body.username;
  student.name  =  req.body.name;
  student.email  =  req.body.email;
  student.password  =  bcrypt.hashSync(req.body.password, salt);

 student.save((err, user) => {
    if (err) return  res.status(500).send('Server error!'); 
        console.log(user);
        const  expiresIn  =  24  *  60  *  60;
        const  accessToken  =  jwt.sign({ id:  user.id }, SECRET_KEY, 
         {
            expiresIn:  expiresIn
        });
        res.status(200).send({ "user":  user, "access_token":  
         accessToken, "expires_in":  expiresIn          
        });

       });
    });
 });
  app.get('/', (req, res) => {
  res.render('index');
 });

app.post('/login', (req, res) => {

const username = req.body.username;
const password = req.body.password;
const student = StudentModel.findOne({ username});
if(!student) {
    return res.json('not found');
}
const isValid =  bcrypt.compareSync(password, student.password);
if (student && !isValid) {
    return  res.json(' found');
}

});

app.listen(port, () => console.log(`Server started on port ${port}`));

signup is working well but when i'm using the login path just to check the username I'm getting this error.注册运行良好,但是当我使用登录路径只是为了检查用户名时,我收到此错误。 I'm not sure why this is happening我不确定为什么会这样

Your code do not have body-parser package - https://expressjs.com/en/4x/api.html#app.post.method .您的代码没有 body-parser 包 - https://expressjs.com/en/4x/api.html#app.post.method If under node_modules/packages it is not present, then install using npm install body-parser --save - https://www.npmjs.com/package/body-parser .如果在 node_modules/packages 下它不存在,则使用npm install body-parser --save - https://www.npmjs.com/package/body-parser进行npm install body-parser --save

// ...code
const SECRET_KEY = "secretkey23456";
const bodyParser = require('body-parser');

const port = process.env.PORT || 3002;
// mongoose coonection
// body Parser Middleware
// app.use(express.json()); --> Incorrect
app.use(bodyParser.json()); 
// app.use(express.urlencoded({ extended: true })); --> Incorrect
app.use(bodyParser.urlencoded({ extended: true }));

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

相关问题 无法读取未定义的属性“用户名” - Cannot read property 'username' of undefined “ErrorTypeError:无法读取未定义的属性‘alg’”JWT - “ErrorTypeError: Cannot read property 'alg' of undefined” JWT 类型错误:无法使用 express-jwt 读取未定义的属性“授权” - TypeError: Cannot read property 'authorization' of undefined using express-jwt 无法读取未定义错误的属性“用户名” - Cannot read property 'username' of undefined error “ typeError:无法读取未定义的属性“用户名”。” - “typeError: Cannot read property 'username' of undefined.” 无法读取未定义的 Javascript/Typescript 的属性“用户名” - Cannot read property 'username' of undefined Javascript/Typescript 类型错误:无法读取未定义的属性“用户名”-- - TypeError: Cannot read property 'username' of undefined-- 无法读取未定义的属性“用户名” - JavaScript - Cannot read property 'username' of undefined - JavaScript TypeError:无法读取未定义的属性“用户名”? - TypeError: Cannot read property 'username' of undefined? TypeError:无法读取password.js中未定义的属性“用户名” - TypeError: Cannot read property 'username' of undefined in passportjs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM