bcrypt.compare 的 nodejs 中未定义用户

users is not defined in nodejs at bcrypt.compare


我正在节点 js 中做以下事情


1.register 并在 mongodb 中保存用户 2.generate 注册时的令牌。 3.authorization 使用令牌 4.login 用户和


这是我的路由器文件


const express = require('express');
const router = express.Router();
const User = require('../models/user');
const bcrypt= require('bcryptjs');
const use_jwt = require('../middleware/use_jwt.js')
const jwt = require('jsonwebtoken');
const user = require('../models/user');
//const user = require('../models/user');
const env = require ('dotenv').config();
router.get('/',use_jwt , async(req,res,next)=>{
    try{
        const user_id = req.user.id;
        const user2 = await User.findById(user_id).select('-password');
        res.status(200).json({
            success:true,
            user:user2
        })
        
    }
    catch(err){
        console.log(err);
        res.status(401).json({
            msg:"server not responding"
            
        })
        
    }
    next();
})



router.post('/register',async(req,res,next)=>{
    const  {username,email,password}=req.body;
    try{
        let user_exist = await User.findOne({email:email});
        if(user_exist){
            return res.status(401).json({
            "success":"false",
            "msg":"user already exist"
            })
        } 
            //importing data to models
            const users = new User();
            users.username = username;
            users.email = email ;
            console.log(users.email);
            console.log(password)
            const salt = await bcrypt.genSalt(10);
            users.avatar =  "https://gravatar.com/avatar/?s=200&d=retro"
            users.password =  await bcrypt.hash(password,salt)
            
            console.log(users.password);
            console.log("hello");
            await users.save();
            const payload = {
                user :{
                    id:users.id
                } 
            }
            console.log(payload.user.id);
            jwt.sign(payload,process.env.jwtUsersecret,{
                expiresIn : 3600000
            },(err,token)=>{
                if(err) throw err;
                else {
                    res.status(200).json({
                        success:true,
                        token:token
                    })
                }
            })
            next();
    } 
    catch(err){
        console.log(err);
    }
});

router.post('/login',async (req,res,next)=>{
    const email = req.body.email
    const password = req.body.password
    console.log('hello2')
    console.log(password)
    console.log('hello1')

    try{
        const user_exist = User.findOne({email:email})
        
        if(!user_exist){
            console.log('one user tried to login without registering')
            res.status(400).json({
                "msg":"pls register and then try to login because there is no such user",
                "sucess":"false"

            })
            
         }
        
            //console.log(user)
            
            
            bcrypt.compare(password, users.password, (err, res) => {
                if (err) throw err;
                if (res) {(req,res)=>{
                    const payload = {
                        user: { 
                            id: users.id
                        }
                    }
                    console.log(payload.user.id);
                    token = jwt.sign(payload,process.env.jwtUsersecret)
                    res.status(200).json({
                        "sucess": "true",
                        "user": user,
                        "email": email,
                        "token": token
                    });
                }
            }
                else {
                    res.json({
                        "success": "false",
                        "msg": "password incorrect"
                    });
                }
                
            })
        }
                
        catch(err){
        console.log(err);
        res.status(400).json({
            "msg":"server not responding"

        })
    }
})
module.exports = router;

但是当我提出 post 请求时。它说用户未定义在

bcrypt.compare(password,users.password)

请为此寻找解决方案。如果这是一个愚蠢或糟糕的问题,我很抱歉。 谢谢

您没有识别“用户”,因此您可以选择为:

User.findOne({email: email}, (err, user)
> {  
> if(err) throw err;
> if(!user){ 
>    console.log("User dose not exisit") 
>  }
> bcrypt.compare(password, user.password , (err, result) => {
>           if (err) throw err;
>           if (result === true) {
>            //User Is Found and password match
>           } else {
>            //User Password dose not match
>           }
>      });
> 
> })

//它会存储找到的用户和小鸡如果用户电子邮件存在如果是它会小鸡密码

希望对你有帮助