Wednesday 31 July 2019

Array issue on MongoDB $in query operator

Hello guys,Pretty new to Node.js and MongoDB, I have an issue with the $in query operator. You have to give it an array, but when I get it from my URL it's not working. Find my code below:// GET /profiles?gender=man:woman router.get('/profiles',auth, async (req, res) => { if(req.query.gender){ const parts = req.query.gender.split(':') parts.forEach(function(element){ console.log(typeof element) console.log(element) }) console.log(Array.isArray(parts)) } // parts = ['man', 'woman'] try { const profiles = await Profile.find({ gender: { $in: parts } }) res.send(profiles) } catch (e) { res.status(500).send() } }) With this code I get the 500 error but everything looks fine in my console:string woman string man true But when I uncomment my variable "parts" and populate it manually with 'man' and 'woman' it works and i get my header status response 200 with the datas.Do you know what's wrong with my code ?

Submitted July 31, 2019 at 11:18AM by Jrm_car

No comments:

Post a Comment