body-parser is not parsing json data

This is how I am using the package:

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

This is the body of my request:

{
    name: "jay",
    price: "12"
}

This is how I am extracting the body:

    const name = req.body.name;
    const price = req.body.price;

But both name and price returned undefined.

EDITED:

According to VS Code, this package is deprecated. But it should still work, no?

Upvotes: 0

Views: 154

Answers (1)

Tomasz Kleszczewski
Tomasz Kleszczewski

Reputation: 56

the request body should be JSON so property names should be in quote signs

{
    "name": "jay",
    "price": "12"
}

Upvotes: 1

Related Questions