0

How could i populate the HTML with simple JSON like:

{
    "id": 34,
    "name": "Tip 33",
    "description": "Tip description 33",
    "created_at": "2018-01-26 18:59:19",
    "updated_at": "2018-01-26 18:59:19"
}

I used the code as:

<ion-content>
  <ion-card *ngFor="let data of searchResults">
    <ion-card-header>
      {{data.name}}
    </ion-card-header>
    <ion-card-content>
      {{data.description}}
    </ion-card-content>
  </ion-card>
</ion-content>

But I get this error as :

Error: Cannot find a differ supporting object '[object Object]' of type 'Tip 33'. NgFor only supports binding to Iterables such as Arrays.

My Code to fetch json is :

 this.apiService.getCareerDescription(data).retry(3)
      .subscribe(data => {
        console.log(data);
        this.loading.dismiss();
        this.searchResults = data;
        //let obj = JSON.parse(this.searchResults);
        //console.log(obj);

        console.log(this.searchResults);
        ;
      }, (err) => {
        this.loading.dismiss();
      }
    );

please help

2 Answers 2

1

For this we have to implement custom pipe.

Pipe:

@Pipe({id : 'key'})
export class custompipe implements PipeTransform {


transform(value, arts:string[]) : any {
let key = [];
for(let key in value){
key.push({key : key, value:value[key]});
}
return key;
}
}

And use it as:

<span *ngFor="#data of SearchResult I key">
Key: {{data.key}} , value: {{data.value}}

Sign up to request clarification or add additional context in comments.

Comments

0

You do not have an array you have a simple object. So *ngFor is not needed here. You can just access the properties via their name. The error says that your searchResults is an object, not array.

<ion-content>
  <ion-card>
    <ion-card-header>
      {{searchResults?.name}}
    </ion-card-header>
    <ion-card-content>
      {{searchResults?.description}}
    </ion-card-content>
  </ion-card>
</ion-content>

4 Comments

i got this ERROR TypeError: Cannot read property 'name' of undefined
Sorry, access via searchResults
You search result comes from an API? Yes?. So try to enforce you with ? symbol. I have updated the post
? will enforce if the object is not undefined or null then access it's property

Your Answer

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