how do i get the data from postman's post api in mongoose and save it in database

Viewed 22

I have a post Api /users/new I'm trying to get the data through postman, I gave the data in body->raw->JSON format but for some reason I cannot access the data from req parameters.

Note: the server is running, and it is successfully connected to the mongo dB database. The get query is also working perfectly. The error message is cannot read properties of undefined.

my post method:

app.post("/users/new", async (req, res) => {
    console.log("post method called")
    const user = new UserModule({
        username: req.body.username,
        gender: req.body.gender,
        email: req.body.email,
        phone: req.body.phone
    });
    try {
        // user.username = req;
        const val = await user.save();
        res.json(val);
        // console.log(req.body)
    } catch (error) {
        console.log(error)
        res.status(500).send(error);
    }
})
1 Answers

Add this line:

app.use(express.json());
Related