I'm currently developing a React app and I've configured a basic Express API to store user details in the database
app.post("/register", jsonParser, (req, res) => {
console.log("body is ", req.body);
let { username, firstname, lastname, password, role, className } = req.body;
password = MD5(password);
console.log(className);
const query = `INSERT INTO users VALUES ('${username}', '${firstname}', '${lastname}', '${password}', '${role}', '${className}')`;
conn.query(query, (err, res) => {
if (err) throw err;
console.log("Registered Successfully");
});
});
and I'm making a POST request using the Fetch API
const response = fetch("http://localhost:8000/register", {
method: "POST",
mode: "no-cors",
body: JSON.stringify({
"username": username2,
"firstname": firstname2,
"lastname": lastname2,
"password": password2,
"role": "student",
"className": "A1",
}),
})
.then((response) => {
console.log(response);
})
.catch((err) => {
console.log(err);
});
After sending the fetch request, I'm seeing this on the server terminal
body is {}
leading to an error in the code
What could be the issue here?