這是我的路由器: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() }})我正在嘗試設(shè)置一個(gè)通過(guò)報(bào)告并返回 newReport 的過(guò)濾器。它將過(guò)濾的條件基于查詢(xún)字符串參數(shù)。因?yàn)椴樵?xún)字符串參數(shù)并不總是存在,所以我需要一種方法來(lái)過(guò)濾條件,僅當(dāng)條件存在時(shí)。想法?
僅當(dāng)變量條件不是未定義時(shí)才過(guò)濾
慕運(yùn)維8079593
2021-06-09 08:24:40