0

I'm getting this error for array of object. My data is coming from database so I cannot change the format of the data.

Here is what my data looks like (coming from database):

dataFromDatabase:any;

this.dataFromDatabase = [
  {"startTime":"Wed Oct 18 2017 12:00:00","endTime":"Wed Oct 18 2017 12:00:00","user_id":"148"},
  {"startTime":"Thu Oct 19 2017 12:00:00","endTime":"Thu Oct 19 2017 12:00:00","user_id":"148"},
  {"startTime":"Fri Oct 20 2017 12:00:00","endTime":"Fri Oct 20 2017 12:00:00","user_id":"148"}
];

What I'm doing in template:

<div>
  <div *ngFor="let times of dataFromDatabase">
    <p>{{times.startTime}}</p>
  </div>
</div>
Goutham Ggs
  • 91
  • 14
  • your code working for me man! – k11k2 Oct 23 '17 at 05:11
  • i'm getting this `error` `ERROR Error: Error trying to diff ` – Goutham Ggs Oct 23 '17 at 05:15
  • make sure that your service call returns the array of object i think its returning objects only – Arun Kumaresh Oct 23 '17 at 05:17
  • the above `object` is `console.log()` of the result from my `service` – Goutham Ggs Oct 23 '17 at 05:19
  • https://stackoverflow.com/questions/38216857/error-trying-to-diff-object-object and @ArunKumaresh mentioned your response is object{} , like `{respone:[{"startTime":"Wed Oct 18 2017 12:00:00","endTime":"Wed Oct 18 2017 12:00:00","user_id":"148"},..]}` could you check web dev tool – k11k2 Oct 23 '17 at 05:25
  • 2
    Change this: `"startTime":"Wed Oct 18 2017 12:00:00"` into: `startTime:"Wed Oct 18 2017 12:00:00"`. If you get the data from an service use `JSON.parse(this.dataFromDatabase)`. Or use `{{times["startTime"]}}` in template. – Swoox Oct 23 '17 at 06:42
  • @Swoox , Thanks It worked for me... Thank you very much.... – Goutham Ggs Oct 23 '17 at 07:04
  • @GouthamGgs your welcome – Swoox Oct 23 '17 at 07:07

1 Answers1

0

This is because *ngFor expects an array but you might be getting an object from the API.Check your response and let me know if there is any other issue.

Akshay
  • 365
  • 1
  • 3
  • 20