I have this code
var ClientSchema = new Schema({
name: {type: String, required: true, trim: true}
});
var Client = mongoose.model('Client', ClientSchema);
Using express, I create a new client with this code
var client = new Client(req.body);
client.save(function(err, data) {
....
});
If I leave the name field empty on the form, mongoose doesn't allow to create the client because I set it as required on the schema. Also, if I leave spaces before and after the name, mongoose delete that spaces before save.
Now, I try to update a client with this code
var id = req.params.id;
var client = req.body;
Client.update({_id: id}, client, function(err) {
....
});
It let me to change the name, but if I leave it empty on the form, mongoose doesn't validate and save an empty name. If I add empty spaces before and after the name, it save the name with spaces.
Why mongoose validate on save but not on update? I'm doing it in the wrong way?
mongodb: 2.4.0 mongoose: 3.6.0 express: 3.1.0 node: 0.10.1