Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to print an JSON string in javascript?

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":"exampleemail@gmail.com","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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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 object

   const body = {
        email: form.elements.email.value,
        password: form.elements.password.value,
   }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading