Mongoogse capture error messages

 

Code:

the require Mongoose = const ( 'Mongoose') 

mongoose.connect ( 'MongoDB: // localhost / test1', {useNewUrlParser: to true}) 
  .then (() => the console.log ( 'database connection success')) 
  .catch ( err => console.log ( 'data connection' + ERR)) 

// set of rules set 
const = PostSchema new new mongoose.Schema ({ 
  title: { 
    type: String, 
    minLength: [4, 'a minimum length of 4'] , 
    MAXLENGTH: [30, 'a maximum length of 30'], 
    TRIM: to true, 
    required: [to true, 'this is required content'] 
  }, 
  Article this article was: { 
    type: String, 
    minLength: [. 5, 'minimum length 5 '], 
    required: [to true,' this must be filled '] 
  } 
}) 
// create and apply a set of rules 
const Post = mongoose.model (' Post ' ,Post scheme)

Post.create ({title: 'Test Test Test', article: 'Content contents'}) 
.then (RES => the console.log (RES)) 
.catch (ERR => { 
  // the console.log (ERR. errors) 
  // loop error information object 
  for (var attr in err.errors) { 
    // print error message 
    console.log ( 'error message:' + err.errors [attr] .message) 
  } 
})

 

Guess you like

Origin www.cnblogs.com/liea/p/11223689.html