index.html
<td>
<img @click="handleLike(item._id)"
:src="item.like?'images/zan.png':'images/no-zan.png'">
</td>
<script>
handleLike(id){
var movies = this.movies;
var item = movies.find(item=>item._id==id);
if(item.like){
item.like = false
}else{
item.like = true;
}
this.movies = movies;
//发送http请求 (like、id)
$.ajax({
url:"http://localhost:8080/doLike",
method:"post",
data:{
id,
like:item.like
}
}).then(res=>{
console.log(res)
})
}
}
ript>
Top250.js
var Top250Schema = new mongoose.Schema({
name: String,
rating:String,
like:Boolean //新增一个Boolean字段
}
index.js
/* 处理点赞 */
router.post("/doLike",async ctx=>{
console.log(ctx.request.body)
var {id,like} = ctx.request.body;
await Top250Model.updateOne({_id:id},{$set:{like}})
})