Home > Blockchain >  Why is POST Request returning wrong data?
Why is POST Request returning wrong data?

Time:09-22

I have a very simple node.js express function

app.post('/userroles', async (req, res) =>
    {
        console.log("save userrole");
        res.json({requestBody: req.body});
        let role = req.body.role;
        console.log(JSON.stringify(role));
        role.id = 1;
        console.log(JSON.stringify(role));
        res.end(JSON.stringify(role));
    })

When my POST body of the request contains

{"role":{"name":"Test","description":"Test","id":null,"functions":[]}}

the first log is showing

{"role":{"name":"Test","description":"Test","id":null,"functions":[]}}

the second log is showing

{"role":{"name":"Test","description":"Test","id":1,"functions":[]}}

but the response body in the browsers developer tools contains

{"requestBody":{"role":{"name":"Test","description":"Test","id":null,"functions":[]}}}

First of all I don't understand where the "requestBody" is coming from, second I don't understand why the id is still null.

Any suggestions?

CodePudding user response:

app.post('/userroles', async (req, res) =>
    {
        console.log("save userrole");
        let role = req.body.role;
        console.log(JSON.stringify(role));
        role.id = 1;
        console.log(JSON.stringify(role));
        res.json(role);
    })
  • Related