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 :