angular-fullstack / generator-angular-fullstack

Yeoman generator for an Angular app with an Express server
https://awk34.gitbook.io/generator-angular-fullstack
6.13k stars 1.24k forks source link

Email Validation always returns error 422 #2705

Open palfaro91 opened 6 years ago

palfaro91 commented 6 years ago
Item Version
generator-angular-fullstack 4.2.2
Node 8.9.2
npm 5.6.0
Operating System OS X 10.12.6
Item Answer
Transpiler Babel
Markup HTML
CSS CSS
Router ui-router
Client Tests Mocha
DB MongoDB
Auth Y
mongoose 4.1.2

Hello! I'm having an issue where I try to update a user's email but the validation always, always returns an error. The error response says "email cannot be blank" but I can see that an email is present and not blank. If I comment out the blank email validation out it then throws the duplicate email validation error even though the email does not exist in the db.

validation code

UserSchema  
  .path('email')  
  .validate(function(email){  
    if(authTypes.indexOf(this.provider) !== -1) { return true; }  
    return !!(email && email.length);  
}, 'Email cannot be blank');

route controller

export function updateContact(req, res){
  if(req.body._id) { Reflect.deleteProperty(req.body, '_id');}  
  let update = new User(req.body);  
  let setUpdate = { $set: { email: update.email, phoneNumber: update.phoneNumber, } };  
  return User.findByIdAndUpdate(req.params.id, setUpdate, {new:true, upsert:true, 
     setDefaultsOnInsert:true, runValidators:true}).exec()  
    .then( (user) => res.json(user); )  
    .catch(validationError(res));  
}

I'm not sure what I am doing wrong.