Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- following: [{ type: mongoose.Schema.Types.ObjectId, ref: 'User' }],
- // ...
- // add user to the followed list
- UserSchema.methods.follow = function(id){
- if (this.following.indexOf(id) === -1){
- this.following = this.following.concat([id]);
- }
- return this.save();
- }
- // remove the user from the followed list
- UserSchema.methods.unfollow = function(id){
- this.following.remove(id);
- return this.save();
- }
- // see if we're following this user
- UserSchema.methods.isFollowing = function(id){
- return this.following.some(function(followId){
- return followId.toString() === id.toString();
- })
- }
- UserSchema.methods.toProfileJSONFor = function (user) {
- return {
- username: this.username,
- bio: this.bio,
- image:
- this.image || 'https://static.productionready.io/images/smiley-cyrus.jpg',
- // true if we're following the given user, false otherwise
- following: user ? user.isFollowing(this._id) : false,
- };
- };
- // register the schema with mongoose as 'User'
- mongoose.model('User', UserSchema);
Add Comment
Please, Sign In to add comment