[SOLVED] Ionic 3 and Angular 4 - Uncaught (in promise): TypeError: Cannot read property 'title' of undefined

@Fieel It seems to me that your error is not because the array is empty, but because some item inside the array is undefined.

You could log your array where you make changes to it to see the items inside it:

.then(result => {
    this.lastWeekData[0] = result; // you only change the array here? you only assign an item to index 0?
    console.log('lastWeekData', this.lastWeekData); // log the whole array
 })

Also, make sure result is defined before you add it to the array:

if (result) {
    this.lastWeekData[0] = result;
}

Or you can filter the array to return only the existing items:

this.lastWeekData[0] = result;
this.lastWeekData = this.lastWeekData.filter(item => !!item);

If you assign an item to an array index, make sure the new index is the one right after the (current) last. If you do the following it could give you problems in certain circumstances:

let a = [];
a[0] = { id: 1 };
a[2] = { id: 3 };
// a.length === 3, a[1] === undefined

Prefer to use push, if possible:

this.lastWeekData.push(result);

Instead of

this.lastWeekData[index] = result;

2 Likes