When I try to print console.log(body.email)
, the console says its undefined, but when I call console.log(body)
it returns me {"email":"[email protected]","password":"Password"}
here is the code below:
document.getElementById("SingupForm").addEventListener('submit', evt =>{
evt.preventDefault()
const form = evt.target
const body =JSON.stringify({
email: form.elements.email.value,
password: form.elements.password.value,
})
console.log(body.email)
})
I’ve tried to rename the strings and add them to an external script, but it didn’t work.
2
Answers
body
is a string, but you’re accessing it like an object.assign it without the calling to
JSON.stringify
if you want to keep it as an objectIn your code body is a string and not an object and that is why you get undifined for the email property.
Try this code: