1

My Web APPI is working ok when I call the GET method on local server ( http://localhost:51085/api/GetMethod ) it retrieves the correct array of objects.

But, when I call it from Angular4 app, the returned observable shows

[object Object] and an error for *ngFor ( ERROR Error: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.).

export class AppComponent { 

   myDesiredDataFromServer$; 

   constructor(private service: ServiceHTTP) { 
      this.myDesiredDataFromServer = service.getData(); 
   }
}

Any help ?

5
  • Can you add your code for Angular 4? Commented Sep 13, 2017 at 11:48
  • can you also post a sample of your api data? Commented Sep 13, 2017 at 11:49
  • if you are returning an observable, use async pipe in ngFor to map it Commented Sep 13, 2017 at 11:55
  • I'm already using a async JayDeeEss : *ngFor="let item of myDesiredDataFromServer $ | async" Commented Sep 13, 2017 at 11:58
  • ok..hope space between myDesiredDataFromServer and $ is a typo and why different names in ts file, with and without $?? Commented Sep 13, 2017 at 12:03

3 Answers 3

1

I think your problem is you're NOT waiting for the response from the server ..

if your service in .ts is returning an Observable<Array<any>>

so try:

   export class AppComponent { 

       myDesiredDataFromServer$; 



 constructor(private service: ServiceHTTP) { 


       service.getData().subscribe((resp)=>{
             this.myDesiredDataFromServer = resp;
        }); 
       }
    }

if it is returning a Promise<Array<any>>:

export class AppComponent { 

       myDesiredDataFromServer$; 

       constructor(private service: ServiceHTTP) { 
          service.getData().then((resp)=>{
             this.myDesiredDataFromServer = resp;
             }); 
       }
    }

if it is returning a async:

 export class AppComponent { 

       myDesiredDataFromServer$; 

       async constructor(private service: ServiceHTTP) { 
         this.myDesiredDataFromServer = await  service.getData();
       }
    }
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you Federico.I'll try it.
let me now if it works .. if you can post also your service code
After subscribing I can see the data array correctly returned at the "_body' property of the return. With service.getData().subscribe((resp)=>{ this.myDesiredDataFromServer$ = ((resp)['_body']); console.log("Ret -> ", this.myDesiredDataFromServer$); }); But can't render with *ngFor -> ERROR Error: InvalidPipeArgument: '[{"Date":"2017-09-13T00:00:00",...etc ( displaying my data array )
0

try

export class AppComponent { 

   public myDesiredDataFromServer$; 

   constructor(private service: ServiceHTTP) { 
      this.myDesiredDataFromServer$ = service.getData(); 
   }
}

Then if it's an observable try the following in your template to print out the full object and not just [object Object]

{{ myDesiredDataFromServer$ | async | json }}

If your ServiceHTTP getData method returned an observable then you should at least be able to see the format of the data you are getting. If you are using it in an *ngFor make sure it's of type array

e.g. If it prints { someKey: [] } then you could run *ngFor="let item of (myDesiredDataFromServer$ | async)?.someKey"

4 Comments

Hi Daniel, my data is at the response _body property but can't render it with *ngFor -> ERROR Error: InvalidPipeArgument: '[{"Date":"2017-09-13T00:00:00", ...etc with my array data
so in your template {{ myDesiredDataFromServer$ | async | json }} shows that object? If so try <div *ngFor="let item of (myDesiredDataFromServer$ | async)">Looped</div>
if {{ myDesiredDataFromServer$ | async | json }} displays {response_body: [{"Date":"2017-09-13T00:00:00}]} then try <div *ngFor="let item of (myDesiredDataFromServer$ | async).response_body">Looped</div>
If the async pipe isn't working at all in your template you can always subscribe to updates in your controller and update a variable that *ngFor loops through. e.g. public myVariable = []; service.getData().subscribe(data => {this.myVariable = data.response_body});, then <div *ngFor="let item of myVariable">looped</div>
0

This worked :

service.getData().subscribe((resp)=>{
    this.myDesiredDataFromServer$ = JSON.parse((resp)['_body']); 

});

enter code here

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.