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