I want to make a register function
I try to do like this:
function finish() {
if (valid) {
const postData = {
email: email.value,
username: username.value,
password: password.value,
password2: password2.value
};
fetch('/register', {
method: 'POST',
headers: {
"Content-type": "aplication/json"
},
body:JSON.stringify(postData)
});
}
}
and in node js i have this:
app.post('/register', async (req, res) => {
try {
const email = req.body.email;
const newuser = req.body.username;
const password = req.body.password;
const repetpasword = req.body.password2;
console.log(newuser);
if (checkvalidinput(email, newuser, password, repetpasword)) {
res.render('startpage')
} else {
const message = 'sumphing is wrong'
res.render('errormessage' , {message: message});
}
} catch (error) {
console.log(error);
}
});
when i try to display in console the variable “newuser” is dispaly “undefined”, how can i solve that?
Any other idea, other metod forsent data from js to node.js?