学node没多久,准备用node做个注册的功能, 但是注册页面的信息提交不到数据库,数据库用的mongodb,
注册页面
<p class="col-sm-8 col-sm-offset-1 main pull-left">
<form class="form-horizontal" action="/signup" method="post" enctype="multipart/form-data">
<p class="form-group">
<label for="inputName" class="col-sm-2 control-label">用户名</label>
<p class="col-sm-4">
<input type="text" class="form-control" id="inputName" name="username">
</p>
</p>
<p class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">密码</label>
<p class="col-sm-4">
<input type="password" class="form-control" id="inputPassword3" name="password">
</p>
</p>
<p class="form-group">
<label for="" class="col-sm-2 control-label">再次输入密码</label>
<p class="col-sm-4">
<input type="password" class="form-control" id="" name="re_password">
</p>
</p>
<p class="form-group">
<label for="inputEmail" class="col-sm-2 control-label">邮箱</label>
<p class="col-sm-4">
<input type="email" class="form-control" id="inputEmail" name="email">
</p>
</p>
<p class="form-group">
<p class="col-sm-offset-2 col-sm-4">
<button type="submit" class="btn btn-default">注册</butto>
</p>
</p>
</form>
</p>
注册页面路由
// 注册页面
router.get('/signup', function(req, res, next){
res.render('sign/signup');
});
router.post('/signup', function(req, res, next){
// 获取数据
var name = req.body.username;
var pwd = req.body.password;
var re_pwd = req.body.re_password;
var email = req.body.email;
// 存入数据库
User.create({
userName: name,
usePwd: pwd,
userEmail: email
},(err) => {
if (err) next (err);
res.redirect('/signin');
})
});
数据模型
const mongoose = require(‘mongoose’);
mongoose.connect(‘mongodb://localhost/cnode’);
var schema = new mongoose.Schema({
name: String,
pwd: Number,
email: String
});
module.exports = mongoose.model(‘User’, schema);
填完注册的信息后,成功的跳到了登录页面,但是这个时候数据库确实这样的
尝试了四次全是这样,这是怎么回事
Remove enctype="multipart/form-data" in the registration form
I seem to see the problem.
First of all,
The attribute names used are userName, usePwd, useEmail,
but in the schema definition it becomes
name
,pwd
,email
The poster’s code is not completely posted. From the existing code, I guess that the field name when defining the schema is inconsistent with the field name of the object passed in when creating the schema instance?