Destructuring object

I got data back from a fetch request and it is a object

 const SendLogin = async () =>{
        
            const res =  await fetch('https://localhost:44332/api/AdminAuth/AdminLogin',{
                  method: 'POST',
                  headers:{'Content-Type': 'application/json'},
                  body: JSON.stringify({
                      email,
                      password
                  })
              });

       if(res.status === 200){
           console.log("Succesful login")
           await res.text().then(function(text){console.log(text)});

the console loge gives:

{"id":26,"accestoken":"3ed3247b-1fc1-462c-9c6b-73f1172876eb","email":"admin@hotmail.com","role":"Admin","name":"Administrator"}

How do i Destructure this object so i can use the information in it?

You don’t need to destructure it - that is just a convenience.

I’m also not sure that is what you need here.

You are using the method text which returns a string, in this case, it looks like JSON. Have you tried the json method?

await res.json().then(function(data){console.log(data)});

If that works, then you have a JS object. If it doesn’t work (for whatever reason) then you need to parse it out with something like JSON.parse.

Once you do that, you should have a standard JS object. I’m assuming that you know how to access that, but if you still have troubles, let us know.
You could just access them as

1 Like

Thank you very much kevin this has solved my question

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.