Thallius
Thallius

Reputation: 2619

Why is POST Request returning wrong data?

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?

Upvotes: 0

Views: 20

Answers (1)

Konrad
Konrad

Reputation: 24661

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

Upvotes: 1

Related Questions