linnovate / mean

The MEAN stack uses Mongo, Express, Angular(6) and Node for simple and scalable fullstack js applications
http://mean.io
12.13k stars 3.45k forks source link

validation error of mongodb #1837

Closed shoaibnoor95 closed 7 years ago

shoaibnoor95 commented 7 years ago

(node:2440) DeprecationWarning: Mongoose: mpromise (mongoose's default promise library) is deprecated, plug in your ow promise library instead: http://mongoosejs.com/docs/promises.html ValidationError: mUser validation failed: phoneNumber: Path phoneNumber is required. at MongooseError.ValidationError (E:\node application\project\node_modules\mongoose\lib\error\validation.js:28:11) at model.Document.invalidate (E:\node application\project\node_modules\mongoose\lib\document.js:1612:32) at E:\node application\project\node_modules\mongoose\lib\document.js:1484:17 at validate (E:\node application\project\node_modules\mongoose\lib\schematype.js:760:7) at E:\node application\project\node_modules\mongoose\lib\schematype.js:805:11 at Array.forEach (native) at SchemaString.SchemaType.doValidate (E:\node application\project\node_modules\mongoose\lib\schematype.js:765:19) at E:\node application\project\node_modules\mongoose\lib\document.js:1482:9 at _combinedTickCallback (internal/process/next_tick.js:73:7) at process._tickCallback (internal/process/next_tick.js:104:9) ValidationError: mUser validation failed: phoneNumber: Path phoneNumber is required. at MongooseError.ValidationError (E:\node application\project\node_modules\mongoose\lib\error\validation.js:28:11) at model.Document.invalidate (E:\node application\project\node_modules\mongoose\lib\document.js:1612:32) at E:\node application\project\node_modules\mongoose\lib\document.js:1484:17 at validate (E:\node application\project\node_modules\mongoose\lib\schematype.js:760:7) at E:\node application\project\node_modules\mongoose\lib\schematype.js:805:11 at Array.forEach (native) at SchemaString.SchemaType.doValidate (E:\node application\project\node_modules\mongoose\lib\schematype.js:765:19) at E:\node application\project\node_modules\mongoose\lib\document.js:1482:9 at _combinedTickCallback (internal/process/next_tick.js:73:7) at process._tickCallback (internal/process/next_tick.js:104:9) ValidationError: mUser validation failed: phoneNumber: Path phoneNumber is required. at MongooseError.ValidationError (E:\node application\project\node_modules\mongoose\lib\error\validation.js:28:11) at model.Document.invalidate (E:\node application\project\node_modules\mongoose\lib\document.js:1612:32) at E:\node application\project\node_modules\mongoose\lib\document.js:1484:17 at validate (E:\node application\project\node_modules\mongoose\lib\schematype.js:760:7) at E:\node application\project\node_modules\mongoose\lib\schematype.js:805:11 at Array.forEach (native) at SchemaString.SchemaType.doValidate (E:\node application\project\node_modules\mongoose\lib\schematype.js:765:19) at E:\node application\project\node_modules\mongoose\lib\document.js:1482:9 at _combinedTickCallback (internal/process/next_tick.js:73:7) at process._tickCallback (internal/process/next_tick.js:104:9)

here is my schema

var userSchema=mongoose.Schema({ firstName: {type: String, required: true}, lastName:{type: String, required: true}, email:{type: String, required:true, unique: true}, password: {type: String, required: true}, phoneNumber:{type: String, required: true,unique: true}, createdAt: {type: Date, default: Date.now}, displayName: {type: String}, area:{type: String}, city: {type: String}, country:{type: String,}, qualification:{type: String}, specialization:{type: String}, sTag1:{type: String}, sTag2:{type: String}, sTag3:{type: String} }); Here how iam getting the data router.post("/signup", function(req,res,next){ var firstname = req.body.firstname; var secondname = req.body.secondname; var password=req.body.password; var email=req.body.email; var phonenumber=req.body.phonenumber;

User.findOne({email: email}, function(err,user){ if(err){return next(err)} if(email){ req.flash("error","already exist"), res.redirect("/signup"); }

var newUser = new User({ firstName: firstname, lastName: secondname, email: email, phoneNumber:phonenumber, password: password }); newUser.save(next); })

chanakaDe commented 7 years ago

DeprecationWarning: Mongoose: mpromise (mongoose's default promise library) is deprecated, plug in your own promise library instead: http://mongoosejs.com/docs/promises.html

This error is telling you to change mongooses default promise lib if you're using promises. But as my knowledge , this shouldn't happen unless you call .then somewhere.

Anyway please try following solution. Add following line where you call MongoDB connection.

mongoose.Promise = global.Promise;

Like this :

core.connect = function connect(opts) {
  mongoose.Promise = global.Promise;
  mongoose.connect(`mongodb://${opts.server}:${opts.port}/${opts.db}`);
  return mongoose.connection;
};
shoaibnoor95 commented 7 years ago

Dear

ValidationError: mUser validation failed: phoneNumber: Path phoneNumber is required. at MongooseError.ValidationError (E:\node application\project\node_modules\mongoose\lib\error\validation.js:28:11) at model.Document.invalidate (E:\node

Iam asking for help about this problem

chanakaDe commented 7 years ago

@shoaibnoor95 Did you get a solution ? If the error still occurs, please try following links. The error is probably with misuse of Mongo objects.

  1. https://github.com/linnovate/mean/issues/509
  2. https://stackoverflow.com/questions/27244849/getting-a-validation-error-in-mongoose-even-though-files-are-provided
  3. https://stackoverflow.com/questions/31663665/mongoose-validation-error-path-is-required

If you can't solve the issue, please provide us the project. Since this is your project based issue, we can't find a common fix for this.

chanakaDe commented 7 years ago

@shoaibnoor95 And also please use standard code formatting to enter a code here. Then it's easy to check. Like this :

var newUser = new User({
    firstName: firstname,
    lastName: secondname,
    email: email,
    phoneNumber:phonenumber,
    password: password
});
newUser.save(next);
});