[英]How can create user in express.js using mongoose
I am trying to create a user in express.js using mongoose.我正在尝试使用 mongoose 在 express.js 中创建一个用户。 I tried everything but i only get lastName and password i don't know what happened.我尝试了一切,但我只得到姓氏和密码,我不知道发生了什么。 even in req.body.firstName also have data in but in newUser i only get two value lastName and password即使在 req.body.firstName 也有数据,但在 newUser 我只得到两个值 lastName 和 password
let newUser = new User({
firstName: req.body.firstName,
lastName: req.body.lastName,
email: req.body.email,
password: req.body.password,
phone: req.body.phone,
})
bcrypt.genSalt(10, (err, salt) => {
bcrypt.hash(newUser.password, salt, (err, hash) => {
if (err) {
res.status(500).json({ error: err});
} else{
newUser.password = hash;
newUser.save()
.then(res => {
res.status(200).json({ message: 'Successfullt created' })
})
.catch((err) => {
res.status(500).json({ error: err});
})
}
})
}) })
Model Model
const mongoose = require('mongoose')
const { Schema } = mongoose
const userModel = new Schema({
firtName: {
type: String,
required: true
},
lastName: {
type: String,
required: true
},
email: {
type: String,
required: true
},
password: {
type: String,
required: true
},
phone: {
type: Number,
required: true
}
})
User = mongoose.model('User', userModel)
module.exports = User
This is a typo.这是一个错字。 firtName
should be firstName
in your model. firtName
应该是 model 中的firstName
。 Try this:尝试这个:
const userModel = new Schema({
firstName: {
type: String,
required: true
},
...
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.