0

Below is my code and what i am trying to do is subscribe to the data i return after the send function is triggered. Now after subscribing, i assign the data return from my API to the this.Data to pass on to my ngFor to display the my data. But i get the error Error trying to diff '[object Object]'. Could it be i am not reaching the display property in the data or not accepting the data in the right format (object)?

Data: DataLink[] = [];


        Send(){

                this.http.forward(this.info)
                 .subscribe( data => {

                     this.Data = data;


                       console.log(data);
                 }) }

    <div *ngFor="let msg of Data">

      {{msg.display}}


**response**
 data: Array
    0: Object
       display: "How are you?"

2 Answers 2

2

Simply loop on the keys directly. you should be fine

Send(){

                this.http.forward(this.info)
                 .subscribe( data => {

              Object.keys(data).forEach(key => {

                  this.Data = data[key];

                       console.log(data);
                 }) }

    <div *ngFor="let msg of Data">

      {{msg.display}}
</div>
Sign up to request clarification or add additional context in comments.

Comments

0

The error you are seeing happens because data is an object and ngFor requires an array.

take a look at this answer: Error trying to diff '[object Object]'

Also, see this for angular http service doc/examples https://angular.io/docs/ts/latest/guide/server-communication.html

4 Comments

am i accessing the display response rightly tho please?
whta's forward? more importantly what is this.http ?
forward is a function in my service.ts
I assume it calls angular's http service get, post ...etc. If that's the case, and you are getting the response you expect from your REST api, then yes, you are doing it correctly. Also, see this for http service doc/examples angular.io/docs/ts/latest/guide/server-communication.html

Your Answer

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