这是我的路由器:
reportRoutes.route('/:id').get(async (req, res) => {
try{
let id = req.params.id
console.log(req.query)
let team = req.query.team
let league_acronym = req.query.league
console.log('team',team)
let regex = new RegExp( id, 'i')
const report = await Report.find({ title: regex })
.populate({path: 'like'})
.populate({
path: 'player',
populate: [{ path: 'team' },
{
path: 'team',
populate: {
path: 'league'
}
}
]
})
if(league_acronym) {
newReport = report.filter((v,i) => {
return v.player.team.league.acronym === league_acronym && v.player.team.team_name === team
})
res.json(newReport)
} else{
res.json(report)
}
} catch (e) {
res.status(500).send()
}
})
我正在尝试设置一个通过报告并返回 newReport 的过滤器。
它将过滤的条件基于查询字符串参数。
因为查询字符串参数并不总是存在,所以我需要一种方法来过滤条件,仅当条件存在时。想法?
相关分类