我有一个userSchema
,它具有以下结构:
const userSchema = new mongoose.Schema({
username: {type:String, required: true, unique: true},
email: {type: String, required: true, unique: true},
password: { type: String, required: true},
我现在正在使用Postman进行测试,我正在提交username, email, password
,如您在下面对象中的RUNNING!
下看到的是帖子中的req.body
,所以我可以看到服务器是什么正在看到。
RUNNING!
{ username: 'TestUser',
email: 'realEmail@obviouslyanemail.com',
password: 'Password' }
{ ValidationError: User validation failed: email: Path `email` is required.
at ValidationError.inspect (/var/www/tradeMentorAPI/node_modules/mongoose/lib/error/validation.js:59:24)
at formatValue (util.js:467:31)
at inspect (util.js:328:10)
at Object.formatWithOptions (util.js:182:12)
at Console.(anonymous function) (console.js:188:15)
at Console.log (console.js:199:31)
at User.register (/var/www/tradeMentorAPI/controllers/authorization.controller.js:19:21)
at /var/www/tradeMentorAPI/node_modules/passport-local-mongoose/index.js:254:20
at /var/www/tradeMentorAPI/node_modules/mongoose/lib/model.js:4518:16
at $__save.error (/var/www/tradeMentorAPI/node_modules/mongoose/lib/model.js:420:16)
at /var/www/tradeMentorAPI/node_modules/kareem/index.js:246:48
at next (/var/www/tradeMentorAPI/node_modules/kareem/index.js:167:27)
at next (/var/www/tradeMentorAPI/node_modules/kareem/index.js:169:9)
at Kareem.execPost (/var/www/tradeMentorAPI/node_modules/kareem/index.js:217:3)
at _handleWrapError (/var/www/tradeMentorAPI/node_modules/kareem/index.js:245:21)
at /var/www/tradeMentorAPI/node_modules/kareem/index.js:272:14
at _next (/var/www/tradeMentorAPI/node_modules/kareem/index.js:94:14)
at process.nextTick (/var/www/tradeMentorAPI/node_modules/kareem/index.js:499:38)
at process._tickCallback (internal/process/next_tick.js:61:11)
errors:
{ email:
{ ValidatorError: Path `email` is required.
at new ValidatorError (/var/www/tradeMentorAPI/node_modules/mongoose/lib/error/validator.js:29:11)
at validate (/var/www/tradeMentorAPI/node_modules/mongoose/lib/schematype.js:844:13)
at /var/www/tradeMentorAPI/node_modules/mongoose/lib/schematype.js:897:11
at Array.forEach (<anonymous>)
at SchemaString.SchemaType.doValidate (/var/www/tradeMentorAPI/node_modules/mongoose/lib/schematype.js:853:19)
at /var/www/tradeMentorAPI/node_modules/mongoose/lib/document.js:1893:9
at process._tickCallback (internal/process/next_tick.js:61:11)
message: 'Path `email` is required.',
name: 'ValidatorError',
properties: [Object],
kind: 'required',
path: 'email',
value: undefined,
reason: undefined,
[Symbol(mongoose:validatorError)]: true } },
_message: 'User validation failed',
name: 'ValidationError' }
控制器:
const makeANewUser = (req, res) => {
const newUser = new User({username: req.body.username, password: req.body.password})
//register is a mongoose method, passing in the user and the password, the it will take care of hashing and all that.
User.register(newUser, req.body.password, (err, user) => {
if (err) {
console.log(req.body);
console.log(err);
return res.send("There was an error and it is...." + err);
alert(error);
}
passport.authenticate('local')(req, res, function () {
console.log(user);
res.send(user.username);
});
});
};
我看到一封电子邮件,为什么猫鼬会给我这个错误?在解决此问题之前,我无法做任何新的事情,这看起来很奇怪。
答案 0 :(得分:1)
对new User
的呼叫没有从请求正文中提取电子邮件字段