Mongoose Getter acting like Setter
I have a getter that looks like this :
UserSchema
.path('avatar')
.get(function(avatar){
if(!avatar){
avatar = "defaultAvatar.jpg";
}
return avatar;
});
It was working really well. Unfortunately I have a big problem with it. When I am creating a new user. Using this method:
var newUser= new UsersModel();
user.name = "John Smit开发者_高级运维h";
user.email = "example@gmail.com";
user.save();
The getter is being triggered and inserting the default "defaultAvatar.jpg" as if it were a setter. Very annoying, really need help on this...
In your schema definition you should use a path modifier like this:
var UserSchema = new Schema({
avatar : { type: String, default: "defaultAvatar.jpg" }
});
精彩评论