【问题标题】:User.find() related errorUser.find() 相关错误
【发布时间】:2018-03-10 20:00:48
【问题描述】:

错误:需要数据和哈希参数

当然,这个问题已经被问过了。但我的问题是不同的。我正在尝试使用已注册到 mongodb 的 username - luckypassword - mani 登录。但是我的项目运行不正常。下面给出了它的 userroutes.js

router.post('/login',(request,response)=>{
console.log(request.body.form_username);//lucky
console.log(request.body.form_password)//mani
userOperation.login(response,request);});

在这里,我在控制台上获取用户名和密码。 现在它正在调用 userOperation.login。这是实现 userOperation.loginusercrud.js。 `

var User = require("../../schema/user/userschema");

var bcrypt = require('bcrypt');

const userOperation={

    login(res,request){
       console.log(request.body.form_username);//lucky
       console.log(request.body.form_password);//mani

              User.findOne({ userid:request.body.form_username}, function (err, user) {
                console.log(request.body.form_username);//undefined....why???
                console.log(request.body.form_password);//undefined....why???
                if (err) {

                     return done(err); }
                if (!user) {

                  return done(null, false, { message: 'no user found' });
                }
                if(user){
                bcrypt.compare(request.body.form_username, user.password, function(err, res) {

                    if (err)
                    throw err;
                    if(!res) {

                      console.log('Ooops!. Wrong Pass!');
                      return done(null, false, req.flash('loginMessage', 'Oops! Wrong password.')); // create the loginMessage and save it to session as flashdata
                    }
                    if(res){
                        // request.session.userid = request.body.form_username;
                        // request.sesssion.password = request.body.form_password;
                    response.send("Login Done");
                    }
                  });
                }
              });
            },
        }`

仍然可以正确打印用户名和密码,但这里 问题开始-- 当我尝试在 User.find() 中打印用户名和密码时,它正在打印 undefind为什么?????? 这就是为什么我收到 错误:需要数据和哈希参数 因为我试图给 bcrypt.compare()

一个未定义的参数

this is link of screenshot of the server console

我的第二个问题是为什么我在服务器启动后四次未定义?? 这是userschema.js

var mongoose = require("../../connection"); var Schema = mongoose.Schema; var userSchema = new Schema({userid:String, password:String
}); var User = mongoose.model("onlineusers",userSchema); module.exports = User;

【问题讨论】:

    标签: node.js mongodb express mongoose mongoose-schema


    【解决方案1】:

    你好朋友可以告诉我你的用户模式。

    如果您在 7.10.0 以上使用 node -v,请使用

    var bcrypt = require('bcryptjs');
    

    如果可能的话,试试这个,我做了同样的事情

     try{
        User.findOne({ email: req.body.email }, function (err, user) {
          if (err){ 
            return res.status(500).send('Error on the server.');
          }
          if (!user){
             return res.status(404).send('No user found.');}
    
          // check if the password is valid
          var passwordIsValid = bcrypt.compareSync(req.body.password, user.password);
          if (!passwordIsValid){
            console.log(passwordIsValid);
             logger.error(req.baseUrl + "status 401 auth faill: "+ req.body.email);
             return res.status(401).send({ auth: false, token: null })
            }
          else{
          // if user is found and password is valid
          // create a token
          var token = jwt.sign({ id: user._id }, config.secret, {
            expiresIn: 86400 // expires in 24 hours
          });
    
          // return the information including token as JSON
          logger.info("status 200 auth success: "+ req.body.email);
          res.status(200).send({ auth: true, token: token });
        }
        });  
      }catch(ex){
        logger.error('Login erro '+ ex)
        console.log(ex);
        throw ex;
    

    也用于适当的正文解析器

    var bodyParser = require('body-parser');
    router.use(bodyParser.json());
    

    如果需要更多帮助,请告诉我很乐意提供帮助:)

    请忽略令牌部分。

    【讨论】:

    • 嘿朋友,我给了userschema.js。
    • 我对正文解析器非常肯定。这是正确的。这就是为什么我能够在控制台上打印用户名和密码
    猜你喜欢
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-23
    相关资源
    最近更新 更多