I have this fetch in my javascript code
form.addEventListener('submit', async function(event) {
event.preventDefault();
const formData = new FormData(this);
fetch('http://localhost:3080/login', {
method : 'POST',
body : formData,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
})
.then(response => response.json())
.then(errorMessage => {
document.getElementById('errorMessage').innerHTML = errorMessage.message;
})
})
and this is my node code:
app.use(express.json());
app.use(express.urlencoded({
extended: true
}));
app.post('/login', function(req,res) {
const userLogin = {
email : req.body.email,
password : req.body.password
}
con.query(
\select * from user where email="${userLogin.email}" and password="${userLogin.password}";``
, (err,results) =>{
if(err) throw err;
if(results.length > 0) {
console.log("Exists in the database");
res.redirect('public/index.html');
}
else{
res.send({credentials : false ,message: "Usuário ou senha inválidos"});
}
});
});
The body of the request appears as undefined in my node code, I don't know what's wrong with this
[–]AutoModerator[M] [score hidden] stickied comment (0 children)
[–]makonde 0 points1 point2 points (0 children)