skip to Main Content

Below is my code to get response from webapi i am able to get results as count is showing right but it coming in [object object] format how to get proper json below is my line of code

enter image description here

const userList: ISuggestionItem[]=[];
      this.props.context.aadHttpClientFactory
      .getClient('xxxxxx-xxx-xxx-xxx-xxxx')
      .then((client: AadHttpClient): void => {
        client
          .get('https://xxxxx.azurewebsites.net/api/xxxxxx?userEmail=xxxxx.onmicrosoft.com', AadHttpClient.configurations.v1)
          .then((response:HttpClientResponse)=> {  
            
            console.log("First hit" + response)
                     response.json().then((responseJSON: any) => {
                      console.log("Second hit" + responseJSON.stringify)
                       responseJSON.map(function(item:any) {
                        console.log("third hit" + item.stringify)
                           userList.push({
                                "key" : item.Test1,
                               "displayValue"  : item.Test2,
                               "searchValue": item.Test3
            
                           });
                           
            
                        }) 
      });
     
    });  
        }); 

2

Answers


  1. [object Object] is the default stringification method of a JS object. You can display a JSON string with JSON.stringify(theObject).

    Login or Signup to reply.
  2. The response JSON is an object.

    If you need to print the JSON object in console, you need to stringify the object.

    Try the below code.

    response=JSON.stringify(response);
    console.log("First hit" + response);
    

    This will work in your case. For more detail refer here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search