用户注册-MEAN堆栈-错误:非法参数:未定义,字符串

时间:2019-01-07 01:15:03

标签: node.js mongodb mean-stack

简介

我目前正在关注Brad Traversy的tutorial(2017年2月制造)来尝试使自己熟悉MEAN堆栈,但遇到了一个我无法解决的问题。使用用于Firefox的RESTED客户端,我正在向/ register发送POST请求。

请求:

{
  "name": "paul",
  "email": "email@email.com",
  "username": "paul",
  "password": "123456"
}

错误

Error: Illegal arguments: undefined, string
    at _async (/home/paulb/Documents/Projects/mean_auth_app/node_modules/bcryptjs/dist/bcrypt.js:214:46)
    at Object.bcrypt.hash (/home/paulb/Documents/Projects/mean_auth_app/node_modules/bcryptjs/dist/bcrypt.js:220:13)
    at bcrypt.genSalt (/home/paulb/Documents/Projects/mean_auth_app/models/user.js:39:12)
    at Immediate.<anonymous> (/home/paulb/Documents/Projects/mean_auth_app/node_modules/bcryptjs/dist/bcrypt.js:153:21)
    at runCallback (timers.js:693:18)
    at tryOnImmediate (timers.js:664:5)
    at processImmediate (timers.js:646:5)

addUser函数

module.exports.addUser = function(newUser, callback){
  bcrypt.genSalt(10, (err, salt) => {
    bcrypt.hash(newUser.password, salt, (err, hash) => {
      if(err) throw err;
      newUser.password = hash;
      newUser.save(callback);
    });
  });
}

路由

router.post('/register', (req, res, next) => {
  let newUser = new User({
    name: req.body.name,
    email: req.body.email,
    username: req.body.username,
    password: req.body.password
  });

  User.addUser(newUser, (err, user) => {
    if(err){
      res.json({success:false, msg:'Failed to Register User'});
    } else {
      res.json({success:true, msg:'User Registered'});
    }
  });
});

用户架构

const userSchema = mongoose.Schema({
  name: {
    type: String
  },
  email: {
    type: String,
    required: true
  },
  username: {
    type: String,
    required: true
  },
  password: {
    type: String,
    required: true
  }
});

const User = module.exports = mongoose.model('User', userSchema);

“调试”

遵循herehere的建议,我将console.log语句(插入到addUser函数的if语句中)。

module.exports.addUser = function(newUser, callback){
  console.log(newUser.password);
  bcrypt.genSalt(10, (err, salt) => {
    bcrypt.hash(newUser.password, salt, (err, hash) => {
      if(err){
        console.log(err);
        console.log(newUser);
        console.log(newUser.password);
        console.log(salt);
      } else {
        newUser.password = hash;
        newUser.save(callback);
      }
    });
  });

哪个返回以下

{ _id: 5c329ce56871660e010323bd }
undefined
$2a$10$yrVVaTXDT3zIe5tV6mneH.

某人here建议这是由于数据库中存在另一个具有相同用户名的用户所致。但是,甚至还没有创建数据库。

> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB

尽管与其“连接”:

[nodemon] restarting due to changes...
[nodemon] starting `node app.js`
Server started on port 3000
Connected to Database: mongodb://localhost:27017/meanauth

此处的代码:

mongoose.connect(config.database);

mongoose.connection.on('connected', () => {
  console.log("Connected to Database: "+config.database);
});

mongoose.connection.on('Error', (err) => {
  console.log("Failed Connecting to Database:  "+err);
});

有人here建议将await应用于saltpassword,但是我不确定在这种情况下该怎么做,或者是否有必要。

我希望上面提供的信息足够。

1 个答案:

答案 0 :(得分:0)

它在错误中说,无效的参数传递给了bcrypt 并说未定义的字符串,可能是您要求的密码没有被加密 使其散列从而错误的方法。 因此,请在控制台日志中查看bcrypt方法之前是否已获取123456作为密码,以便您可以确保密码可用于bcrypt进行哈希处理 我很确定你在那里会变得不确定。

module.exports.addUser = function(newUser, callback){console.log(newUser.password); // try this 
  bcrypt.genSalt(10, (err, salt) => {
    bcrypt.hash(newUser.password, salt, (err, hash) => {
      if(err) throw err;
      newUser.password = hash;
      newUser.save(callback);
    });
  });
}

在您的模型中

const userSchema = mongoose.Schema({
  name: {
    type: String
  },
  email: {
    type: String,
    required: true
  },
  username: {
    type: String,
    required: true
  },
  password: {
    type: String,
    required: true
  }
});

const User = module.exports = mongoose.model('user', userSchema ); //have you done this