Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //...
- favorites: [{type: mongoose.Schema.Types.ObjectId, ref: 'Article' }],
- },
- {timestamps: true}
- );
- UserSchema.plugin(uniqueValidator, {message: 'is already taken.'});
- // push article id to favorites list and save the state of the user
- UserSchema.methods.favorite = function (id) {
- if (this.favorites.indexOf(id) === -1) {
- this.favorites = this.favorites.concat([id]);
- console.log('favorites after favoriting', this.favorites);
- console.log('article id', id);
- }
- return this.save();
- };
- // remove article from favorites list and save state
- UserSchema.methods.unfavorite = function(id){
- this.favorites.remove(id);
- return this.save();
- }
- //...
Advertisement
Add Comment
Please, Sign In to add comment