I wrote a simple mongoose query to sort the DB and display the result, but i wanted to it dynamically so i wrote
exports.sortIt = (req,res,next)=>{
const sortby = JSON.stringify(req.params.sortBy)
var storeArr=[]
dine.find({}).sort({sortby:-1}).then((data)=>{
console.log(sortby)
storeArr.push(...data)
console.log(storeArr.length)
if(storeArr.length == 0){
res.status(404).send(storeArr)
}
else{
res.status(200).send(storeArr)
}
}).catch((err)=>{
res.status(404).send("error occurred")
})}
Nothing much fancy, however it is not returning the sorted data, it just returns me all of the database, but whenever i hardcode my "sortby" in the query, it returns the correct data.
Can you suggest me what i am doing wrong over ?
My query is correct, there must me something in parsing of "sortBy" from request