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

How do I create a new instance of a Schema and save it to the database?

我有一个只有两个字符串的 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 表单时,它只是挂起

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