I have a use case to search for a particular string on one field and return the results in sorted order based on another field. The below is the function I'm using in Node.js and the error being thrown.
router.get("/getappts/:username", function (req, res) {
console.log(req.params.username)
collection.find({ username: req.params.username }).sort( {date : 1} ), function (err, appointments) {
if (err) throw err;
console.log(appointments)
res.json(appointments);
}
})
Error - collection.find(...).sort is not a function.
Not sure how to model the query.
But the below query on Mongo Compass seems to work fine -
might be where your callback is, I would suggest using promises instead
router.get("/getappts/:username", async function (req, res) {
try {
console.log(req.params.username)
const appointments = await collection.find({ username: req.params.username }).sort({ date : 1 })
console.log(appointments)
res.json(appointments);
} catch (err) {
throw err;
}
}