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

时间:2020-05-14 18:22:04

标签: node.js

这是“登录”过程的代码,我想返回一个“令牌”,但是当我发送请求时,我看到此错误,我该如何解决

在控制台中(出现错误): 登录 ,NourGeorge @ gmail.com ,1923552 ,✖TypeError:无法读取未定义的属性“ hashedPassword” ,POST /用户/登录401 48.376毫秒-87

我从邮递员发送此请求:

    POST http://localhost:4000/user/login
    Content-Type: application/json
    {
         "email":"NourGeorge@gmail.com",
          "password":"1923552"
    }

我从邮差那里得到了这个答案:

     {
         "accessToken": null,
         "message": "User login error,Invalid Information(Invalid Password)"
     }

userController.js:

////Login 
const login = async (req, res, next) => {
console.log('LogIn');
let { email, password } = req.body;
console.log(email)
console.log(password);
// in findUserByEmail Compaire password with password in database
const result = await User.findUserByEmail(email, password);
//if the password is correct => generate a token using JWT
if (result == 1) {
    res.status(200).json({
        message: 'Process succeeded'
    });
    // generate a token using JWT
    const accessToken = jwt.sign({ id: user.uid }, process.env.ACCESS_TOKEN_SECRET,
        {
            expiresIn: 3600   //1 hour
        })
    // res.json({accessToken:accessToken})
    // return user information & access token 
    res.status(200).send({
        id: user.uid,
        email: user.email,
        // password: user.password,
        accessToken: accessToken
    })
}
else {
    res.status(401).json({
        accessToken: null,
        message: 'User login error,Invalid Information(Invalid Password)'
        // error: 'User login error,Invalid Information(Invalid Password)'
    });
}
}

userModel.js:

 static async findUserByEmail(email, password) {
    const Log_I=await SingletonSqlHandler.instance.sendQuery(`SELECT * FROM 
              GetUserByEmail('${email}')`)
    .then(result => {
        let userData = result.recordset[0];
        const areMatched = PasswordHasher.verify(password, userData.hashedPassword);
        if (areMatched) {
            Printer.Logger.print("Successfully logged in", Printer.Colors.green, 
  Printer.Symbols.ok, true);

           return 1;
        } else {
            Printer.Logger.print('Password is incorrect', Printer.Colors["error message"], 
  Printer.Symbols.err, true);
        }
        return 0;
    }).catch(err => {
        Printer.Logger.print("" + err, Printer.Colors["error message"], Printer.Symbols.err, 
  true);
            return -1;
    });
    return Log_I;
   }
   }

userRouter.js:

  const router = require('express').Router();
  const userController = require('../Controllers/userController');
  router.post('/login',userController.login);
  module.exports = router;

app.js:

     const express = require('express');
     const morgan = require('morgan');
     const bodyParser = require('body-parser');
     const https=require('https');
     const request=require('request');
     // Modules Variable
     const app = express();
     // User import
     const usersRoutes = require('./routes/userRoute');
     const productsRoutes=require('./routes/productRoute')
     app.use(express.json()) 
     app.use(morgan('dev')); // Logger middleware
     app.use(bodyParser.urlencoded({extended: false})); // Parsing body middleware
     app.use(bodyParser.json({limit: '500mb'})); // Parsing JSON objects
     // Adding headers
     app.use((req, res, next) => {
     res.header('Access-Control-Allow-Origin', '*'); // Allowing reqeusts from everywhere
     res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, 
     Authorization');
     if (req.method === 'OPTIONS') {
     res.header('Access-Control-Allow-Methods', 'PUT, POST, PATCH, DELETE, GET');
     return res.status(200).json({});
     }
     next();
     });
     // Detecting routes
     app.use('/user', usersRoutes);
     app.use('/product', productsRoutes);
     // Handling invalid routes
     app.use((req, res, next) => {
     const error = new Error('Not found');
     error.status = 404;
     next(error);
     });
     app.use((error, req, res, next) => {
     res.status(error.status || 500);
     res.json({
     error: {
        message: error.message
     }
     });
     });
     module.exports = app;

0 个答案:

没有答案