I am working with node js/MongoDB to fetch all posts and checked whether the user already like or not for this particular post if liked then add a key "isLiked" in each object. But unable to do that.
Post.find().exec(function(err, posts) {
if (err) {
return res.status(500).json({ message: err.message });
}
var result = posts.map(isLiked(req.user));
console.log(result);
return res
.status(200)
.json({ message: "Successfully fetch all posts", result: result });
});
var isLiked = function(user) {
return function(x) {
var o = Object.assign({}, x);
o.isLiked = Post.find(
{
_id: x._id,
likes: user
},
function(err, isliked) {
if (isliked != "") {
return 1
} else {
return 0;
}
}
);
return o;
};
};