Home > Enterprise >  Unable to make put request using node js. Mongoose
Unable to make put request using node js. Mongoose

Time:07-30

i am just looking to make an PUT request using Mongoose database. But Its Unable to make any request. I am using postman to pass the data, but no response.

script.js

app.route("/articles/:articleTitle")
    .put(function (req, res) {
        Article.updateMany(
            { title: req.params.articleTitle },
            { title: req.body.title, content: req.body.content },
            { overwrite: true },
            function (err) {
                if (!err) {
                    res.send("Successfully Updated The Data !");
                }
            }
        );
    });

here is the code i am using to pass the PUT request in my localhost server but unable to do so.

No Response Here Is The Result

CodePudding user response:

You don't send any response in case of an error, causing the request to hang and never return. Change it to e.g.:

function (err) {
    if (!err) {
        res.send("Successfully Updated The Data !");
    } else {
      console.log(err);
      res.status(500).send("Update failed due to error "   err.message);
    }  
}

CodePudding user response:

I would prefer you to have the proper nodejs code structure. Mixing routes, services and controllers will led to big chuck of codes that is big confusing and unreadable.

Recommended nodejs project structure:-

  1. helpers
  2. middlewares
  3. controllers
  4. services
  5. routes
  6. models
  • Related