1.对于多个雷同的表 可以封装其相同部分

  1. const router=require("koa-router")();
  2. const MovieModel=require("../../models/movie")
  3. router.get("/api/movie",async ctx=>{
  4. const {m}=ctx.query;
  5. try{
  6. var data=await MovieModel(m).find();
  7. console.log(data.length);
  8. if(data.length){
  9. ctx.body={
  10. code:200,
  11. res:data,
  12. msg:m
  13. }
  14. }else{
  15. throw new Error("输入字段不合法")
  16. }
  17. }catch(err){
  18. ctx.body={
  19. code:400,
  20. msg:"输入字段不合法"
  21. }
  22. }
  23. })
  24. module.exports=router;
  1. const {mongodb,Schema} = require("./baseDB/db-movies");
  2. const bean = require("./Schema/MoviesSchema")
  3. const MovieSchema = new Schema(bean);
  4. function MovieModel(table){
  5. return mongodb.model(table,MovieSchema,table)
  6. }
  7. module.exports = MovieModel;