获取喜欢的post的ID – Node.js Express Socket.io

我试图build立一个职位系统,人们可以评论和喜欢。 这就是我现在所做的

<!-- posts.ejs --> <% posts.forEach(function(task) { %> <%- include post.ejs %> <% }) %> 

当有人喜欢,我使用这样的sockets

 <!-- 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(); }; 

我的问题是如何知道喜欢的职位的ID(task._id是未定义的)? 如果你有更好的解决scheme,让我知道。

您不会将任何值传递给其他模板文件。

 <% posts.forEach(function(task) { %> <%- include post.ejs %> <% }) %> 

需要更像是:

 <% posts.forEach(function(task) { %> <%- include('post.ejs', { task: task }) %> <% }) %> 

可能还有比这更多的东西,但让我知道这是否有效,如果没有,我会看看是否还有其他build议。