illcrx
illcrx

Reputation: 814

Mongoose Schema missing values and showing errors

I have a userSchema that has this structure:

const userSchema = new mongoose.Schema({
    username: {type:String, required: true, unique: true},
    email: {type: String, required: true, unique: true},
    password: { type: String, required: true},

I am using Postman right now for testing and I am submitting username, email, password as you see under RUNNING! in the object below is the req.body from the post so I can see what the server is seeing.

RUNNING!
{ username: 'TestUser',
  email: '[email protected]',
  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' }

Controller:

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);
        });
    });
};

I am seeing an email so why would Mongoose be giving me this error? I cannot do anything new until this is resolved, seems weird.

Upvotes: 0

Views: 226

Answers (1)

ack_inc
ack_inc

Reputation: 1103

The call to new User isn't picking up the email field from req.body

Upvotes: 1

Related Questions