I just saw a blog on how to retrive a particular document by the ID of the document but here the code just does not seem to work for some reason. It works always, can you people tell me why this is happening?
Code:
app.post('/api/get-list-data', (req, res) => {
const listID = req.body.listID;
client.connect(async err => {
const collection = client.db('to-do-lists').collection('made');
const data = await collection.findOne({ _id: new ObjectId() })
if(data) {
res.send(data);
}
else {
res.send({ success: false });
}
})
})
Any help would be greatful!
4
Answers
I fixed my issue by getting the ObjectId in the top of the page like this:
ObjectId
is a global inmongo-cli
but not in thenode
environment.You can use exported function
ObjectId
from themongodb
package to create a newObjectId
instance.You may also pass a valid hex representation of an objectID as an argument (assuming that listID is one)
You also need to import the
mongodb
package before using the statement above for this to work.Try to follow example:
Reference:
I am using this library
https://www.npmjs.com/package/bson-objectid