我想发出带有一些来自请求正文的信息的后处理请求,但这给了我{ValidatorError:路径email
是必需的。并输入名称和密码
//schema
const userSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
password: {
type: String,
required: true
},
email: {
type: String,
required: true
},
avatar: {
type: String
},
date: {
type: Date,
default: Date.now
}
});
//route
router.post("/register", (req, res) => {
User.findOne({ email: req.body.email }).then(user => {
if (user) {
res.status(400).json({ email: "email already exists" });
} else {
const avatar = gravatar.url(req.body.email, {
s: "200",
r: "pg",
d: "mm"
});
const newUser = new User({
name: req.body.name,
password: req.body.password,
email: req.body.email,
avatar
});
bcrypt.genSalt(10, (err, salt) => {
bcrypt.hash(newUser.password, salt, (err, hash) => {
// if (err) throw err;
newUser.password = hash;
newUser
.save()
.then(user => res.json(user))
.catch(err => console.log(err));
});
});
}
});
});