Answer the question
In order to leave comments, you need to log in
How to sort by different parameters in an API request?
Hey! I want to make it in my online store so that on a single page for the product below there are similar ones, but for this I need to sort the products on the server by parameters (for example, where gender = male and productType = 'T-shirt'). Here is what I was able to write:
router.get('/sort/:sortBy',(req,res,next) => {
const sort = req.params.sortBy
Product.find({ gender : req.body.gender})
.then(response => {
res.status(200).json(response)
})
.catch(err => {
res.status(500).json({
error: err
})
})
})
Answer the question
In order to leave comments, you need to log in
Probably like this?
Product.find({
gender : req.body.gender,
productType: req.body.productType
})
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question