I'm struggling with a for-loop and promises in my angular2 project. I have multiple methods that return promises, and after these promises are resolved, I want to populate an array in the class using Promise.all(variable).then(function(result){.......}; However, when I try to access the array within the Promise.all, the console throws an error
core.es5.js:1084 ERROR Error: Uncaught (in promise): TypeError: Cannot set property 'item' of undefined
...
public item;
allItems = [];
public method() {
var promises = [];
this.dbService.getItem('myKey', 'table')
.then((data) => {
this.myArrayNumber = data;
for (let i = 0; i < this.myArrayNumber.length; i++) {
promises.push(this.dbService.getItem(this.epodLiefernr[i], 'lieferungen'));
}
Promise.all(promises)
.then(function (result) {
for (let i = 0; i < result.length; i++) {
this.item = result[i];
}
});
...
Why am I unable to access this.item at that point? Can anyone provide me with suggestions on how to solve this issue?