1.对于多个雷同的表 可以封装其相同部分
const router=require("koa-router")();
const MovieModel=require("../../models/movie")
router.get("/api/movie",async ctx=>{
const {m}=ctx.query;
try{
var data=await MovieModel(m).find();
console.log(data.length);
if(data.length){
ctx.body={
code:200,
res:data,
msg:m
}
}else{
throw new Error("输入字段不合法")
}
}catch(err){
ctx.body={
code:400,
msg:"输入字段不合法"
}
}
})
module.exports=router;
const {mongodb,Schema} = require("./baseDB/db-movies");
const bean = require("./Schema/MoviesSchema")
const MovieSchema = new Schema(bean);
function MovieModel(table){
return mongodb.model(table,MovieSchema,table)
}
module.exports = MovieModel;