About Me

My photo
I am a senior MEAN/MERN stack consultant for United Nations. With 7 years of experience.

Monday, April 12, 2021

How to use a api response object...


this is an angular code, 
where it calls a certain service, 
and read the value in 3 different ways. 

lets see..

the code ...

    this.lookupService.getLookupData('Session').subscribe(
      data => {
        let sessionLookupData1 = data;
        console.log('sessionLookupData1'sessionLookupData1._body);

        let sessionLookupData2 = JSON.stringify(data);
        console.log('sessionLookupData2'sessionLookupData2._body);

        let sessionLookupData3 = JSON.parse(JSON.stringify(data));
        console.log('sessionLookupData3'sessionLookupData3._body);
      }
    );

1st sessionLookupData1 will print the the response of the api call, so you want be able to access the _body 

2nd sessionLookupData2 will print the response (Converts a JavaScript value to a JavaScript Object Notation (JSON) string.),  so you wont be able to get the objects as well. 

3rd is the correct way. Converts a JavaScript Object Notation (JSON) string into an object.

Now you can access the object values just as accessing a normal js object. 

Thanks you :)
happy coding. JavaScript rules ...