I'm trying to setting up a posts system where people will be able to comment and like. This is what I did right now
<!-- posts.ejs -->
<% posts.forEach(function(task) { %>
<%- include post.ejs %>
<% }) %>
When someone likes, I use socket like this
<!-- post.ejs -->
<script>
var socket = io('localhost:48001');
$('#like').on('click', function(event){
var task = <% task._id %>
console.log(task);
var liker = {
pId: task ,
usr: user._id,
liker: 1
};
socket.emit('like', liker, function(response){
if(response==true){
//console.log("blue");
}
});
});
//server side
exports.like = (pId, usr) => {
Post.findOneAndUpdate({_id :pId}, {$inc : {'likers' : 1}}).exec();
};
My question is how can I know the id of the liked post (task._id is undefined)? If you have a better solution, let me know.