0

I'm trying to delete something but the current format below doesn't work.

It works when I input it manually and write "_id":ObjectId("Idhere"). It doesn't even work when I use JSON.stringify. It also works in postman.

Does someone see what I'm missing?

 router.delete('/wishlist', (req, res) => {
    const db = mongoUtil.getDb();
   
    db.db("mern-auth-2").collection("savedbooks").deleteOne({
         _id:ObjectId(req.body._id),
    }) 
 });
Elizabeth
  • 157
  • 1
  • 15
  • Since you are using Mongo with `express`, I suggest using the [mongoose](https://www.npmjs.com/package/mongoose) driver instead of the native MongoDB Node.js drive – Picoral May 31 '21 at 02:08
  • You need to create an instance of the `ObjectId`. See [this](https://stackoverflow.com/a/12901901/10893256) answer for reference. – Picoral May 31 '21 at 02:13

2 Answers2

0

change the code from :

db.db("mern-auth-2").collection("savedbooks").deleteOne({
     _id:ObjectId(req.body._id),
})  

to :

db.db("mern-auth-2").collection("savedbooks").deleteOne({
     _id: req.body._id,
}) 

hope it would help

Irwan
  • 104
  • 8
0

Try this Code

    db.db("mern-auth-2").collection("savedbooks").deleteOne({
         _id:req.body._id
    }) 
Majid Hajibaba
  • 3,105
  • 6
  • 23
  • 55
Sumit Pathak
  • 86
  • 1
  • 2