-2

I am getting an error while trying to loop through and display data using ngFor.

Actual error in console "Error: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays."

Here is my code:

//TS Component

var lssessionsObject = JSON.parse(localStorage.getItem('sessionsObject'));

//localstorage array

lssessionObject = {"total_rows":1,"offset":0,"rows":[{"id":"245fd1cbf38f79256a7443a0b5001461","key":"245fd1cbf38f79256a7443a0b5001461","value":{"cinemaid":"0001","session":"168970","filmid":"HO00003858","screen":"VIP 1","datetime":"2017-07-22T11:00:00","attributes":["3D Film","Scene VIP"]}}]}

HTML

<ion-slides class="image-slider" slidesPerView="3" pager="true">
        <ion-slide *ngFor="let item of sessionsObject" class="border">
        <button ion-item color="primary" id={{item.filmid}} class="bottom-slider">
            {{item.filmid}}
        </button>

    </ion-slide>
</ion-slides>

Please assist me

skydev
  • 1,867
  • 9
  • 37
  • 71

1 Answers1

1

ngFor works with iterables. An iterable object is the object that implements [Symbol.iterator] method that returns an iterator. You can read more about it here. Only Array and Map are iterable in JS by default.

Objects in JavaScript are not iterable. And your sessionsObject is simple object and it therefore doesn't implement [Symbol.iterator] method. But you can easily implement it yourself:

// 
lssessionsObject[Symbol.iterator] = function* () {
   for (p in this) yield this[p];
}
Max Koretskyi
  • 101,079
  • 60
  • 333
  • 488