0
votes

Hello i am using mongoose to get data from mongodb. I have the app deployed on heroku. When i run my app locally i get the validation error if the user exist. If i run it on heroku i get the validation error but the app is also crashing too.

This is my post request to create a user.

    exports.create = function (req, res, next) {
      var newUser = new User(req.body);
      console.log(newUser);
      newUser.provider = 'local';
      newUser.role = 'user';
      newUser.save(function(err, user) {
        if (err) return res.status(400).json(err);
        var token = jwt.sign({_id: user._id }, config.secrets.session, { expiresIn: 60*5 });
        res.json({ token: token });
      });
    };     

This is the mongoose validation.

  UserSchema
    .path('email')
    .validate(function(value, respond) {
      var self = this;
      this.constructor.findOne({email: value}, function(err, user) {
        if(err) throw err;
        if(user) {
          if(self.id === user.id) return respond(true);
          return respond(false);
        }
        respond(true);
      });
  }, 'The specified email address is already in use.');

This is the error:

"stack": "Error\n at MongooseError.ValidationError (/Users/panayiotis/WebstormProjects/SmartHome/node_modules/mongoose/lib/error/validation.js:22:16)\n at model.Document.invalidate (/Users/panayiotis/WebstormProjects/SmartHome/node_modules/mongoose/lib/document.js:1162:32)\n at /Users/panayiotis/WebstormProjects/SmartHome/node_modules/mongoose/lib/document.js:1037:16\n at validate (/Users/panayiotis/WebstormProjects/SmartHome/node_modules/mongoose/lib/schematype.js:653:7)\n at /Users/panayiotis/WebstormProjects/SmartHome/node_modules/mongoose/lib/schematype.js:678:11\n at Query. (/Users/panayiotis/WebstormProjects/SmartHome/server/api/user/user.model.js:80:16)\n at /Users/panayiotis/WebstormProjects/SmartHome/node_modules/mongoose/node_modules/kareem/index.js:177:19\n at /Users/panayiotis/WebstormProjects/SmartHome/node_modules/mongoose/node_modules/kareem/index.js:109:16\n at nextTickCallbackWith0Args (node.js:420:9)\n at process._tickCallback (node.js:349:13)", "message": "User validation failed", "name": "ValidationError", "errors": { "email": { "properties": { "type": "user defined", "message": "The specified email address is already in use.", "path": "email", "value": "[email protected]" },

1
Im not sure though, but have you tried to return the last respond(true); in your validation?CENT1PEDE

1 Answers

0
votes

I solve it updating the mongoose