I have tried all answers. But it doesn't work for me. Maybe if you are getting params with express. It's worked for me. Try this;
var MongoClient = require('mongodb').MongoClient;
const {ObjectId} = require('mongodb');
app.get('/:id', (req, res) => {
var reqId = req.params.id
var query = reqId.toString()
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db("AllDB");
dbo.collection("Events").findOne({_id: ObjectId(query)}, function(err, result) {
if (err) throw err;
res.send(result)
db.close();
});
});
})
**Update :**
Hey, I give same error again, and I notice new solution. If your code like this:
app.get('/', (req, res) => {
res.send('TEST')
})
app.get('/:id', (req, res) => {
var reqId = req.params.id
var query = reqId.toString()
})
Could you try change your get request route. Maybe such as app.get('/findid/:id)
It worked for me. I was searching again and i found this way.
Thanks, Regards.