Search code examples
mongodbexpressmongoose

deleteOne() function not working in mongoose


I created a basic RESTful API using node,express,mongoDB and mongoose.Postman says that the delete action was completed but It doesn't gets removed from the database

after mongoose removed callbacks it got very confusing

here is the code

.delete(function(req,res){
    // console.log(req.params.articleTitle)
    Article.deleteOne({title: req.params.articleTitle})
    res.send('Deleted')
  })


Solution

  • Without seeing any of your other code or error messages I'm going to go out on a limb and say you are moving from callbacks to modern async/await pattern and you are having a few issues migrating. Let me know if this works:

    .delete(async function(req,res){ //< Make callback async
        // console.log(req.params.articleTitle)
        try{
           const deletedArticle = await Article.deleteOne({title: req.params.articleTitle}) //< await the deletion
           res.send('Deleted')
        }catch(err){
           console.log(err);
           //Handle error
        }
    });