I set up my mongoose model, and want to get the top k searched objects based on one of the field, "numberOfSearched". when I passed a number into the limit function, it works perfectly. But I want it to be dynamic that it returns a number of objects based on the value indicated in the endpoint parameters, but it keeps return all of them when I replace the number with "req.params.number". Any spot check would be appreciated!! Thanks ahead
object,.route.js
// Getting top k
router.get("/top-k/:number", async (req, res) => {
Word.find({})
.sort({ numberOfSearched: -1 })
.limit(req.params.number)
.exec(function (err, docs) {
if (err) {
return res.status(500).send({ message: err.message });
} else {
res.status(200).send(docs);
}
});
});
CodePudding user response:
You will just have to add plus before req.params.number whatever you pass into url it will be consider as string use req.params.number or parseInt(req.params.number)