Is there a way to retrieve specific data from my MongoDB database based on the URL? For example, if I navigate to localhost:3000/phones, I want to fetch all entries with the category "phones", and if it's localhost:3000/laptops, I need data related to laptops.
Here is the schema for the data:
name: {
required: true,
type: String
},
category: {
required: true,
type: String
},
Currently, my code looks like this:
router.get('/getAll', async (req, res) => {
try {
const data = await Model.find();
res.json(data)
}
catch (error) {
res.status(500).json({ message: error.message })
}
})
I attempted to use findByID
method but encountered some issues.