I'm a newbie in to node.js and mongodb. Im using express for rest-api.
I'm trying to create an API where user can post and like. I'm able to create post API and the issue is with like API where the user can click the like button and it will be counted and when he clicks the like button again it will take as unlike and update the same in the backend server (express and mongodb) in my case.
Schema Definition
module.exports = mongoose.model('posts', new Schema({
post: { type: String, required: true },
date: { type: Date, required: true, default: moment.now() },
author: { type: String, required: true },
authorId: {type: String, required: true},
likes: {type: Object, required: true, default: []},
likesCount: { type: Number, required: true, default: 0},
dislikes: { type: Object, required: true, default: [] },
dislikesCount: { type: Number, required: true, default: 0 },
comments: { type: Object, required: true, default: [] },
commentsCount: { type: Number, required: true, default: 0 }
}, { timestamps : { createdAt: 'created_at' , updatedAt: 'updated_at' } }));
Code:
router.post('/post/likes', function(req, res) {
if (!_.isEmpty(req.body.postId) && !_.isEmpty(req.body.user) && !_.isEmpty(req.body.userId)) {
//Finding the post by Id
Posts.findById(req.body.postId, function(err, posts) {
if (err) {
return res.send(500).json({
success: false,
msg: err
});
}
if (posts !== null) {
var user = {
userId: req.body.userId,
user: req.body.user
};
//Returns the matched obj if true
//Returns undefined if false
var alreadyLiked = _.find(posts.likes, function(obj) {
if (obj.userId === user.userId && obj.user === user.user) {
return true;
} else {
return false;
}
});
if (alreadyLiked === undefined) {
posts.likes.push(user);
posts.likesCount = posts.likes.length;
var updatedPost = new Posts(posts);
updatedPost.save(function(err, data) {
if (err) {
return err;
}
res.status(200).send({
success: true,
data: data,
message: 'You have liked the post!'
});
});
} else {
//Removing the already liked user object from posts.likes
posts.likes = _.without(posts.likes, _.findWhere(posts.likes, user));
posts.likesCount = posts.likes.length;
posts.markModified('likesCount');
var reupdated = new Posts(posts);
reupdated.save(function(err, data) {
if (err) {
return err;
}
res.status(200).send({
success: true,
data: data,
message: 'You have unliked the post!'
});
});
}
} else {
res.status(200).send({
success: false,
message: 'No post found!'
});
}
});
} else {
res.status(400).send({
success: false,
message: 'Bad request value'
});
}
});
The issue is when I like the post for first time it works perfect and returns the expected response.
img: liked-response-img
when i unlike the post by calling the same API it returns some unexpected result. The user who unliked is removed from the likes property but the count still remains '1' as shown in img below. I can't figure out why? Can someone please point out where and what I'm doing wrong. Thanks in advance!
img: unliked-response-img
how about just making posts.save()
or posts.save().exec()
instead of creating a new "copied" post and saving that.