Answer the question
In order to leave comments, you need to log in
How to sort through a post request by several parameters?
Hey! I decided to make it so that I have similar ones under a single product, but for this I need to make a sorting router according to different parameters (after asking, I realized what I need to do through a post request). The names of the parameters are entered in the body into the sortBy array and then somehow filtered from all products through find the necessary products.
router.post('/sort',(req,res,next) => {
const sort = req.params.sortBy; // массив который был в body
Product.find({ gender : req.body.gender}) //вместо 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
First, understand how a query is executed in MongoDB by several parameters, then substitute your values (by which you should filter). Test, if everything worked correctly, then you will understand everything.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question