skip to Main Content

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

2

Answers


  1. 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);
        }  
    }
    
    Login or Signup to reply.
  2. 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);
            }
        });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search