[英]how to add user's information in mongoose database?
i have a registration form in .jade format 我有.jade格式的注册表
form(method='post', action='/users/register', enctype='multipart/form-data')
.form-group
label Name
input.form-control(name='name', type='text', placeholder='Enter Name' required)
.form-group
label Email
input.form-control(name='email', type='email', placeholder='Enter Email' required)
.form-group
label Username
input.form-control(name='username', type='text', placeholder='Usernamee' required)
.form-group
label Password
input.form-control(name='password', type='password', placeholder='Enter Password' required)
.form-group
label Password
input.form-control(name='password2', type='password', placeholder='Confirm Password' required)
.form-group
label Profile Image
input.form-control(name='profileimage', type='file')
input.btn.btn-default(name='submit', type='submit', values='Register')
and this is how i'm creating a new user and trying to add them to the database but the data is not being added into the database. 这就是我创建新用户并将其添加到数据库中的方法,但是数据并未添加到数据库中。 The problem is it's not taking values from the form fields: name, email username, password are all NULL I don't understand why as it's supposed to take all values
问题是它没有从表单字段中获取值:名称,电子邮件用户名,密码都为NULL我不知道为什么,因为它应该采用所有值
router.post('/register', function(req, res, next) {
var newUser = new User({
name: req.body.name,
email: req.body.email,
username: req.body.username,
password: req.body.password,
profileImage: profileImageName
});
}
You need a body parsing middleware to parse incoming request bodies. 您需要一个主体解析中间件来解析传入的请求主体。
Express has a built-in middleware function (which is based on the body-parser
module) to parse urlencoded bodies since v4.16.0 onwards. 从v4.16.0开始,Express具有内置的中间件功能 (基于
body-parser
模块)来解析经过urlencoded的主体。 But this built-in parser does not handle multipart bodies. 但是,此内置解析器无法处理多部分主体。
Since you're trying to handle a multipart/form-data
, you may need a middleware like multer . 由于您尝试处理
multipart/form-data
,因此可能需要像multer这样的中间件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.