0

So I'm trying to get single ID of a document, I tried something like this, before it worked, but since new version update it says that imported mongo from mongodb does not exist. This means that mongo library doesn't longer exist, is there any other way of doing this?

So here is my following response which I want to send:

    app.get ('/products/:id', async (req , res) => {
    let id = req.params.id;
    let db = await connect();
    console.log("params: ",id);
    let doc = await db.collection("posts").findOne({_id: mongo.ObjectId(id)});
    console.log(doc)
    res.json(doc)
});

I used this: import mongo from 'mongodb'; and when I try to get single ID from a document it says: Cannot read property 'ObjectId' of undefined.

Jan Tuđan
  • 233
  • 3
  • 17
  • A more minimal example would be `import mongo from 'mongodb'; console.log(mongo);`. If you don't see output, the import has failed. The question title would then become: "How do I import mongo?". Getting a value from the database probably works fine once you've got the library – danh Aug 17 '21 at 22:35
  • @danh yeah sorry for the non-specific expression, but the thing is that `import mongo from 'mongodb'` worked before for me and now it doesn't, it says it's undefined when I do `console.log`, so is there any new way of doing this? – Jan Tuđan Aug 17 '21 at 22:43
  • Is it a node project? If so, does it mention mongo in package.json? Have you done an npm install? Also see answers here: https://stackoverflow.com/questions/35962879/how-to-import-mongodb-using-es6-style-imports – danh Aug 17 '21 at 23:02
  • @danh yeah it's node project I'm doing this with Vue framework. Yes I've done that, I already have data on mongo database, I used this before too but it seems that something changed. – Jan Tuđan Aug 17 '21 at 23:06
  • I tried with `require` and it still doesn't work. – Jan Tuđan Aug 17 '21 at 23:11

0 Answers0