У меня есть 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},
Я сейчас использую Почтальон для тестирования, и я отправляю 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);
});
});
};
Я вижу электронное письмо, так почему бы Mongoose дал мне этоошибка?Я не могу сделать ничего нового, пока это не решено, кажется странным.