const router = require("koa-router")();
const MovieModel = require("../../models/movie");
router.get("/api/search",async ctx=>{
var {keyworld} = ctx.query; ##搜索关键字
var reg = new RegExp(keyworld);
var tables = ["top250","inTheaters","comingSoon"]; ##1.将三张表定义成一个数组
var searchMovies = []
for(let item of tables){ ##对数组进行for-of遍历
var res = await MovieModel(item).find({title:reg}); ##传值
searchMovies.push(...res); ##将搜索到三张表中的内容合成一个数组
}
ctx.body = {
code:200,
res:searchMovies,
total:searchMovies.length,
msg:"电影搜索"
}
})
module.exports = router;