Piyush Shrivastava
Piyush Shrivastava

Reputation: 33

Unable to make put request using node js. Mongoose

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

Upvotes: 1

Views: 185

Answers (3)

Onyemachi Agwu
Onyemachi Agwu

Reputation: 1

.put(function (req, res) {
    Article.updateMany(
      { title: req.params.articleTitle },
      { title: req.body.title, content: req.body.content }
    )
      .then(function (articles) {
        res.send(articles);
      })
      .catch(function (err) {
        res.send(err);
      });
  });

Upvotes: 0

app.route("/articles/:articleTitle")
    .put(async function (req, res) {
        try {
            await Article.updateMany(
                { title: req.params.articleTitle },
                { title: req.body.title, content: req.body.content },
                { overwrite: true },
            )
            res.status(200).send("Successfully Updated The Data !");
        } catch (err) {
            res.status(500).send("Update failed due to error " + err.message);
        }
    });

Upvotes: 0

eol
eol

Reputation: 24565

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);
    }  
}

Upvotes: 2

Related Questions