Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Suppose we have 2 required propertiestitle and content

var ArticleSchema = new Schema({
  created: {
    type: Date,
    default: Date.now
},
  title: {
    type: String,
    default: '',
    trim: true,
    required: 'Title cannot be blank'
},
  content: {
    type: String,
    default: '',
    trim: true,
    required: 'Content cannot be blank'
},
  user: {
    type: Schema.ObjectId,
    ref: 'User'
}
});

At the moment, when I call the $save function, the server stops after the first error and returns a string with 'Title cannot be blank'. Is there a way to let mongoose return a JSON object of all failed validations?

    // Create new Article
    $scope.create = function() {
        // Create new Article object
        var article = new Articles({
            title: this.title,
            content: this.content
        });

        // Redirect after save
        article.$save(function(response) {
            //success
        }, function(errorResponse) {
            $scope.error = errorResponse.data.message;
            //Desired output
            // errors :{
            //   "Title" : "Title cannot be blank",
            //   "Content" : "Content cannot be blank"}
        });
    };

Is the issue on the server-side (should I tweak Mongoose model?) or on the client-side (should I tweak Angular $save function's errorResponse?) ?

EDIT: a screenshot of the errorResponse object : console.dir(errorResponse)

share|improve this question
1  
What does errorResponse.errors output? Can you console.dir that? –  WakeskaterX yesterday
    
Does this make sense to do that, in general? Say, I have title required and content's length > title's length. If title is empty, how are you gonna validate against content (if I passed in a string)? –  ABOS yesterday
    
I added a screenshot of the errorResponse object. It does not hold an errors property. ABOS, yes it does make sense, you can just add an if condition for when title is empty –  Steph Lhm yesterday

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.