如何创建Schema的新实例并将其保存到数据库?

时间:2016-01-27 20:33:51

标签: javascript express mongoose user-registration

我有一个UserSchema只有两个字符串:用户名和密码。

我正在尝试通过HTML表单向数据库添加新用户。

我有一条路线:'/ signup'

这就是我所拥有的:

app.post('/signup', function(req, res){
  // Hash the password
  var salt = bcrypt.genSaltSync(10);
  var hash = bcrypt.hashSync(req.body.password, salt);

  User.create({username: req.body.username, password: hash}, function (err, user) {
    console.log("signing up...");
     if (err){
       console.log("something went wrong");
       return res.redirect('/signup');
     }
     saved!
  });
});

我一直在关注各种不同的教程和视频。从调试我知道salt / hashing很好。这是导致问题的创造。当我提交HTML表单时,它只是挂起

1 个答案:

答案 0 :(得分:0)

我刚刚意识到,当它成功时,我不会在任何地方重定向。在重定向中添加,现在可以使用。