'Post received through axios in react is undefined in nodejs

I am sending user login info with axios in react to the nodejs server. However, my data in nodejs is undefined.

Here is my react js code: -

const reqData = { user: loginID, password }
const loginResult = await axios.post('/login', reqData, {
                headers:  {
                    'Content-Type': 'application/json'
                }
            }
        )

Here is my nodejs code: -

app.post("/login", (req, res) => {
    const data = req.body;
    console.log("Data received: ", data);
    res.send("OK")
})

Every time I submit my form, I get undefined in my nodejs server.



Solution 1:[1]

it might be possible because your body object isn't a valid json. Try posting something like

const reqData = { 
   user: loginID, 
   pass: password 
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 amit.s19