开发者

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" }
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜