2

I am getting [object object] showing in my html page.

I am calling the a web service and in my console.log. I can see the full response but on my template I only see [object object]

test() {

 this.http.post('api...', JSON.stringify(this.getAllCaseInfoWithCAS)).subscribe(data => {  
  this.testData = [data];
 });

}

I have a ngFor

<ng-template ngbPanelContent>
 <div class="row" *ngFor="let item of testData">  
   {{item.getAllCaseInfoWithCASReturn.caseDetails}}
 </div>
</ng-template>

The response looks like this

{
    "getAllCaseInfoWithCASReturn": {
        "caseDetails": {
            "caseNumber": "12345",
            "componentDetails": {
                "component": "29"
            },
            "personNumber": "5",
            "month": "7"
        }
    }
}

How can I display all the information on the template?

11
  • try using {{item.getAllCaseInfoWithCASReturn.caseDetails.toString()}} Commented Nov 2, 2018 at 8:08
  • 2
    {{ item.getAllCaseInfoWithCASReturn.caseDetails | json }}. Also, read the guide about the HttpClient. You don't need to call JSON.stringify. HttpCLient does that for you, and sets the correct content-type header. Commented Nov 2, 2018 at 8:10
  • @SarthakAggarwal I still get the same issue Commented Nov 2, 2018 at 8:10
  • Why are you making the result as an array explicitly (this.testData = [data]), can you provide an example with more than one caseDetails if you have that? Also explain how you want to display the data. Commented Nov 2, 2018 at 8:15
  • @sabithpocker If I don't do [data] then I get this error "Cannot find a differ supporting object of type 'object'". I just want to display the data in a list(not important at the moment how it shows). I only have 1 case at this point in time. Commented Nov 2, 2018 at 8:18

2 Answers 2

4

You can use JsonPipe

{{ value_expression | json }}

Converts a value into its JSON-format representation. Useful for debugging.

{{ item.getAllCaseInfoWithCASReturn.caseDetails | json }}
Sign up to request clarification or add additional context in comments.

Comments

0

Better start by making JSON formatted as an array of caseDetails, here is a rough code of what you can do with current JSON:

// This should be refactored to a service file.
getCaseDetails(data: any): Observable<any[]> {
    const url = `api/url`;
    return this.httpClient.post(url, data)
        .pipe(
            map(result => result['getAllCaseInfoWithCASReturn']['caseDetails']),
            tap(caseDetail => console.log(caseDetail))
        )
}

test() {
    this.getCaseDetails(dummyData).subscribe(caseDetail => {
        this.testData = caseDetail;
    })
}

<ng-template ngbPanelContent>
 <div class="row">  
   {{testData?.caseNumber}} <!-- or json pipe -->
   {{testData?.personNumber}}
   {{testData?.month}}
   {{testData?.componentDetails?.component}}
 </div>
</ng-template>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.