I'm learning mongoose and tried setting "trim" to true inside a mongoose schema. However it is not working as expected.
I've tried setting other things like "lowercase" to true and it does work, so I don't know why "trim" isn't working.
var userSchema = {
name: {type: String, required: true, trim: true, lowercase: true},
email: {
type: String,
required: true,
validate: function(value){
if(!(validator.isEmail(value))){
throw new Error("Not a valid email address");
}
},
trim: true,
},
age: {
type: Number,
validate: function(value){
if(value < 0){
throw new Error("Age must be a positive number");
}
},
default: 0
},
password: {
type: String,
required: true,
minlength: 7,
validate: function(value){
if(value.toLowerCase().includes("password")){
throw new Error(" Passwords should not contain the word
'password ' ");
}
},
trim: true
}
}
var User = mongoose.model('User', userSchema);
var someuser = new User({
name: "some user",
age: 25,
email: "user@something.com",
password: "verysecurepassword"
})
I expected an the name of the new user to be 'someuser', but instead it turned out to be 'some user'.
Name "some user" has space in the middle of the string.
What you are trying to do will not work as trim
will remove whitespaces from start and end of string only.