index.html

  1. <td>
  2. <img @click="handleLike(item._id)"
  3. :src="item.like?'images/zan.png':'images/no-zan.png'">
  4. </td>
  5. <script>
  6. handleLike(id){
  7. var movies = this.movies;
  8. var item = movies.find(item=>item._id==id);
  9. if(item.like){
  10. item.like = false
  11. }else{
  12. item.like = true;
  13. }
  14. this.movies = movies;
  15. //发送http请求 (like、id)
  16. $.ajax({
  17. url:"http://localhost:8080/doLike",
  18. method:"post",
  19. data:{
  20. id,
  21. like:item.like
  22. }
  23. }).then(res=>{
  24. console.log(res)
  25. })
  26. }
  27. }
  28. ript>

Top250.js

  1. var Top250Schema = new mongoose.Schema({
  2. name: String,
  3. rating:String,
  4. like:Boolean //新增一个Boolean字段
  5. }

index.js

  1. /* 处理点赞 */
  2. router.post("/doLike",async ctx=>{
  3. console.log(ctx.request.body)
  4. var {id,like} = ctx.request.body;
  5. await Top250Model.updateOne({_id:id},{$set:{like}})
  6. })