Error: Uncaught (in promise): TypeError: Cannot read property 'undefined' of undefined

I have got this error on my search function, but I am unable to resolve. Any idea? The error seems to be in the line that has let day = data.schedules[dayIndex];

Error: Uncaught (in promise): TypeError: Cannot read property 'undefined' of undefined
TypeError: Cannot read property 'undefined' of undefined
    at http://localhost:8100/build/main.js:110181:37
    at t.invoke (http://localhost:8100/build/polyfills.js:3:8971)
    at Object.onInvoke (http://localhost:8100/build/main.js:4625:37)
    at t.invoke (http://localhost:8100/build/polyfills.js:3:8911)
    at r.run (http://localhost:8100/build/polyfills.js:3:4140)
    at http://localhost:8100/build/polyfills.js:3:13731
    at t.invokeTask (http://localhost:8100/build/polyfills.js:3:9655)
    at Object.onInvokeTask (http://localhost:8100/build/main.js:4616:37)
    at t.invokeTask (http://localhost:8100/build/polyfills.js:3:9576)
    at r.runTask (http://localhost:8100/build/polyfills.js:3:4831)
    at c (http://localhost:8100/build/polyfills.js:3:13190)
    at c (http://localhost:8100/build/polyfills.js:3:12876)
    at http://localhost:8100/build/polyfills.js:3:13722
    at t.invokeTask (http://localhost:8100/build/polyfills.js:3:9655)
    at Object.onInvokeTask (http://localhost:8100/build/main.js:4616:37)
    at t.invokeTask (http://localhost:8100/build/polyfills.js:3:9576)
    at r.runTask (http://localhost:8100/build/polyfills.js:3:4831)
    at o (http://localhost:8100/build/polyfills.js:3:1891)
    at IDBRequest.invoke (http://localhost:8100/build/polyfills.js:3:10673)

My models

[
  {
    "id": 1,
    "day": 1501282800000,
    "entries": [
      {
        "id": 2,
        "name": "DAY 1 ENTRY 2",
        "minTimeValue": "10:15:00",
        "maxTimeValue": "12:00:00",
        "events": [
          {
            "id": 2,
            "name": "SHORT COURSES\t\t\t\t\t",
            "startTimeValue": "09:15:00",
            "endTimeValue": "11:00:00",
            "description": "Dont go yet, we still have a lot for your, stay sitdfa",
            "speakers": [
              
            ],
            "venues": [
              {
                "id": 2,
                "name": "Diamond",
                "description": "This is  good room"
              },
              {
                "id": 1,
                "name": "Grand Ball room",
                "description": "A very good venue"
              }
            ],
            "tracks": [
              {
                "id": 2,
                "trackName": "Production",
                "description": "This is another beautiful track",
                "color": "Red"
              }
            ],
            "interactive": true,
            "fav": 0
          },
          {
            "id": 1,
            "name": "ONSITE REGISTRATION",
            "startTimeValue": "08:00:00",
            "endTimeValue": "09:00:00",
            "description": "This is a simple event, just to let us know what an event looks like",
            "speakers": [
              
            ],
            "venues": [
              {
                "id": 3,
                "name": "Iroko suite",
                "description": "This is a nice room"
              }
            ],
            "tracks": [
              
            ],
            "interactive": false,
            "fav": 0
          }
        ]
      },
      {
        "id": 3,
        "name": "DAY 1 ENTRY 3",
        "minTimeValue": "13:00:00",
        "maxTimeValue": "18:00:00",
        "events": [
          {
            "id": 5,
            "name": "Nffa jnadption",
            "startTimeValue": "16:15:00",
            "endTimeValue": "18:00:00",
            "description": "Ok, now I am tired, test data can be very difficult to generate",
            "speakers": [
              {
                "id": 1,
                "authToken": "4179fc9a-f29e-419d-ab74-9420679a3468",
                "pushToken": "ccccc",
                "emailAddress": "07@gmail.com",
                "firstName": "xxx",
                "lastName": "xxx",
                "creationDate": 1498588023949,
                "picture": null,
                "subjectRoles": [
                  {
                    "id": 1,
                    "roleName": "SPEAKER"
                  }
                ],
                "subjectPermissions": [
                  
                ],
                "profile": "This is the profile for Adeyemi Adetayo, easy going man that believes in God",
                "social": "@adeyemiadetayo07",
                "company": "Shell Petroleum Develoment Company"
              },
              {
                "id": 2,
                "authToken": "xxxxx",
                "pushToken": null,
                "emailAddress": "xxe@yahoo.com",
                "firstName": "Frank",
                "lastName": "Donga",
                "creationDate": 1498588153758,
                "picture": null,
                "subjectRoles": [
                  {
                    "id": 1,
                    "roleName": "SPEAKER"
                  }
                ],
                "subjectPermissions": [
                  
                ],
                "profile": "Another clear example of an easy going person, he's very nice",
                "social": "@frankdonga",
                "company": "dsfmdsf"
              }
            ],
            "venues": [
              {
                "id": 1,
                "name": "Grand Ball room",
                "description": "A very good venue"
              },
              {
                "id": 3,
                "name": "Iroko suite",
                "description": "This is a nice room"
              }
            ],
            "tracks": [
              {
                "id": 1,
                "trackName": "Drilling Engineering",
                "description": "This is a nice track",
                "color": "Blue"
              }
            ],
            "interactive": false,
            "fav": 0
          },
          {
            "id": 4,
            "name": "Key club",
            "startTimeValue": "14:00:00",
            "endTimeValue": "16:00:00",
            "description": "Dont know what this is, sounds boring though, maybe we have to wait till ATCE to find out",
            "speakers": [
              {
                "id": 2,
                "authToken": "e7d775e7-7a99-445d-a970-4ac9db6749d6",
                "pushToken": null,
                "emailAddress": "xvxcvxc@yahoo.com",
                "firstName": "Frank",
                "lastName": "Donga",
                "creationDate": 1498588153758,
                "picture": null,
                "subjectRoles": [
                  {
                    "id": 1,
                    "roleName": "SPEAKER"
                  }
                ],
                "subjectPermissions": [
                  
                ],
                "profile": "Another clear example of an easy going person, he's very nice",
                "social": "@frankdonga",
                "company": "Syre systems Nigeria Limited"
              }
            ],
            "venues": [
              {
                "id": 2,
                "name": "Diamond",
                "description": "This is  good room"
              }
            ],
            "tracks": [
              {
                "id": 1,
                "trackName": "Drilling Engineering",
                "description": "This is a nice track",
                "color": "Blue"
              }
            ],
            "interactive": false,
            "fav": 0
          },
          {
            "id": 3,
            "name": "Leadership Workshop",
            "startTimeValue": "11:15:00",
            "endTimeValue": "13:00:00",
            "description": "This is an event that lets you know the power of thefamn.afma,, for interactive event, just click, scroll down, and join the conversation",
            "speakers": [
              {
                "id": 1,
                "authToken": "4179fc9a-f29e-419d-ab74-9420679a3468",
                "pushToken": "cx,vmxc,v,
                "emailAddress": "axvxb@gmail.com",
                "firstName": "Asgsg",
                "lastName": "Asgsga",
                "creationDate": 1498588023949,
                "picture": null,
                "subjectRoles": [
                  {
                    "id": 1,
                    "roleName": "SPEAKER"
                  }
                ],
                "subjectPermissions": [
                  
                ],
                "profile": "This is the profile formn,zmdfzc easy going man that believes in God",
                "social": "@acvxcv",
                "company": "Shell Petroleum Develoment Company"
              }
            ],
            "venues": [
              {
                "id": 2,
                "name": "Diamond",
                "description": "This is  good room"
              }
            ],
            "tracks": [
              {
                "id": 3,
                "trackName": "Economics",
                "description": "This is one very very very good track",
                "color": "Green"
              }
            ],
            "interactive": true,
            "fav": 0
          },
          {
            "id": 2,
            "name": "SHORT COURSES\t\t\t\t\t",
            "startTimeValue": "09:15:00",
            "endTimeValue": "11:00:00",
            "description": "Dfgsdgsdgsdfafsf",
            "speakers": [
              
            ],
            "venues": [
              {
                "id": 2,
                "name": "Diamond",
                "description": "This is  good room"
              },
              {
                "id": 1,
                "name": "Grand Ball room",
                "description": "A very good venue"
              }
            ],
            "tracks": [
              {
                "id": 2,
                "trackName": "Production",
                "description": "This is another beautiful track",
                "color": "Red"
              }
            ],
            "interactive": true,
            "fav": 0
          }
        ]
      }
    ]
  }
]

PROVIDERS

  getSchedules(): Promise<Schedule[]>{
        return this.storage.get('schedules').then((value) => {
            return this.schedules=value;
        });
  }

  hasFav(id): Promise<boolean> {
      return this.storage.get("favEvents").then((value)=>{
            value.indexOf(id) > -1
            return true;
    })
  }

getTimeline(dayIndex: number, queryText = '', excludeTracks: any[] = [], segment = 'all'):Promise<boolean> {
    return this.getSchedules().then((data: any) => {
      let day = data.schedules[dayIndex];
      day.shownEvents = 0;

      queryText = queryText.toLowerCase().replace(/,|\.|-/g, ' ');
      let queryWords = queryText.split(' ').filter(w => !!w.trim().length);

      day.entries.forEach((entry: any) => {
        entry.hide = true;

        entry.events.forEach((event: any) => {
          // check if this event should show or not
          this.filterEvent(event, queryWords, excludeTracks, segment);

          if (!event.hide) {
            // if this event is not hidden then this entry should show
            entry.hide = false;
            day.shownEvents++;
          }
        });

      });

      return day;
    });
  }

  filterEvent(event: any, queryWords: string[], excludeTracks: any[], segment: string) {

    let matchesQueryText = false;
    if (queryWords.length) {
      // of any query word is in the event name than it passes the query test
      queryWords.forEach((queryWord: string) => {
        if (event.name.toLowerCase().indexOf(queryWord) > -1) {
          matchesQueryText = true;
        }
      });
    } else {
      // if there are no query words then this session passes the query test
      matchesQueryText = true;
    }

    // if any of the sessions tracks are not in the
    // exclude tracks then this session passes the track test
    let matchesTracks = false;
    event.tracks.forEach((trackName: string) => {
      if (excludeTracks.indexOf(trackName) === -1) {
        matchesTracks = true;
      }
    });

    // if the segment is 'favorites', but event is not a user favorite
    // then this event does not pass the segment test
    let matchesSegment = false;
    if (segment === 'favorites') {
      if (this.hasFav(event.name)) {
        matchesSegment = true;
      }
    } else {
      matchesSegment = true;
    }

    // all tests must be true if it should not be hidden
    event.hide = !(matchesQueryText && matchesTracks && matchesSegment);
  }

And the page TS code.

updateSchedule() {
    // Close any open sliding items when the schedule updates
    this.scheduleList && this.scheduleList.closeSlidingItems();

    this.scheduleProvider.getTimeline(this.dayIndex, this.queryText, this.excludeTracks, this.segment).then((data: any) => {
      this.shownEvents = data.shownEvents;
      this.entries = data.entries;
    });
  }

THE VIEW

  <ion-toolbar no-border-top color="primary">
    <ion-searchbar color="light"
                   [(ngModel)]="queryText"
                   (ionInput)="updateSchedule()"
                   placeholder="Search">
    </ion-searchbar>
  </ion-toolbar>

Kindly help.

N:B - I am taking a clue from the ionic team conference app

Are you certain that storage is ready before you try to interact with it?

Yes, it is. What do you think could be it?

Anyone, please, kindly offer a solution to this.

console.log(data); in front of the line that you think is causing problems.

1 Like

Thanks a lot. That was eventually what I did to discover my error.

1 Like

Perfect.

Can you share what exactly solved your specific problem? Then you can select that post as the “solution” and this topic will be marked as solved.

Whoot! Sorry I am just returning to the platform after a long time.

After console.log() each step, I observed that there was no item as “schedules” in my object array. Simply put, I was looping wrongly.

Thanks for the tip. @Sujan12

1 Like