1.实现电影收藏状态的切换
2.实现对user表collects的添加
// UserModel--user.js
const {mongodb,Schema} = require("./baseDB/db-movies");
const schema = new Schema({
name:String,
collects:Array
});
var UserModel = mongodb.model("user",schema,"user");
module.exports = UserModel;
// movieCollect.js
const router = require("koa-router")();
const MovieModel = require("../../models/movie");
const userModel = require("../../models/user");
router.post("/api/collectMovie/:m",async ctx=>{
// console.log(ctx.request.params); //获取动态路由传参
var {m} = ctx.request.params;
var {id,collected} = ctx.request.body;
try{
var data = await MovieModel(m).updateOne({_id:id},{$set:{collected}});
if(data.nModified==0){
throw new Error( `${m}这个传值不合法`)
}else {
//添加收藏到user表
var item = await MovieModel(m).find({_id:id});
await userModel.updateOne({},{$push:{collects:item[0]}});
}
}catch(err){
ctx.body = {
code:400,
msg:"输入参数不合法"
}
}
})
module.exports = router;