# Angular 2 Deep Dive Promise Foreach loop

**URL:** <https://forum.ionicframework.com/t/angular-2-deep-dive-promise-foreach-loop/80370>\
**Category:** ionic-v3\
**Created:** [February 21, 2017, 9:50am UTC](https://forum.ionicframework.com/t/angular-2-deep-dive-promise-foreach-loop/80370 "2017-02-21T09:50:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hichman](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/hichman/32/107292_2.png) [@Hichman](https://forum.ionicframework.com/u/Hichman)\
**Post date:** [February 21, 2017, 9:50am UTC](https://forum.ionicframework.com/t/angular-2-deep-dive-promise-foreach-loop/80370/1 "2017-02-21T09:50:35Z")

</div>

Hi,  
How can I have a resolve in foreach loop (for each element of an array, try to resolve a problem)  
code:  
serverIntervention.forEach(function (row){  
this.interventionService.getById(row.int\_id).then(  
(result) =\>{console.log("int exist "+this.commonService.stringfyJson(result))},  
(error) =\>{console.log("int not exist "+this.commonService.stringfyJson(error))}  
);

I have an error : Error: Uncaught (in promise): TypeError: Cannot read property ‘interventionService’ of undefined  
TypeError: Cannot read property ‘interventionService’ of undefined  
knowing that my service is injected correctly (I have already used it)  
PS: same error with `for( let row of serverIntervention)`  
Thank you,

---

<div class="post-metadata">

**Author:** ![Hichman](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/hichman/32/107292_2.png) [@Hichman](https://forum.ionicframework.com/u/Hichman)\
**Post date:** [February 21, 2017, 11:03am UTC](https://forum.ionicframework.com/t/angular-2-deep-dive-promise-foreach-loop/80370/2 "2017-02-21T11:03:08Z")

</div>

Finally I create a method add all in remote service which loop correctly

---

<div class="post-metadata">

**Author:** ![mich356c](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/mich356c/32/105794_2.png) [@mich356c](https://forum.ionicframework.com/u/mich356c)\
**Post date:** [February 21, 2017, 12:48pm UTC](https://forum.ionicframework.com/t/angular-2-deep-dive-promise-foreach-loop/80370/3 "2017-02-21T12:48:19Z")

</div>

> [@Hichman](#):
>
> serverIntervention.forEach(function (row){

Just an FYI, I’m pretty sure this is the cause. Should be

```auto
serverIntervention.forEach(row => {

```

---

<div class="post-metadata">

**Author:** ![Hichman](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/hichman/32/107292_2.png) [@Hichman](https://forum.ionicframework.com/u/Hichman)\
**Post date:** [February 21, 2017, 1:40pm UTC](https://forum.ionicframework.com/t/angular-2-deep-dive-promise-foreach-loop/80370/4 "2017-02-21T13:40:38Z")

</div>

> [@mich356c](#):
>
> serverIntervention

serverIntervention is an array 🙂

---

<div class="post-metadata">

**Author:** ![mich356c](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/mich356c/32/105794_2.png) [@mich356c](https://forum.ionicframework.com/u/mich356c)\
**Post date:** [February 21, 2017, 1:46pm UTC](https://forum.ionicframework.com/t/angular-2-deep-dive-promise-foreach-loop/80370/5 "2017-02-21T13:46:45Z")

</div>

I know, but because you’re using `function() {}` and not the arrow functions (`() => {}`), `this` is now the **function** , of which `serverIntervention` doesn’t exist on. The only thing I changed in the previous post was the `function (row)` -\> `row =>`

Along with some of the other things you’re trying to access on `this`

```auto
serverIntervention.forEach(function (row) {
// ^^^^^^^^
// Marked vars below wont be found because of the above.
// The function is replacing this with itself
	this.interventionService.getById(row.int_id).then(
	// ^^^^^^^^^^ wont be found
		result => {
			console.log("int exist "+this.commonService.stringfyJson(result));
			// ^^^^^^^^^^ wont be found
		},
		error => {
			console.log("int not exist "+this.commonService.stringfyJson(error));
			// ^^^^^^^^^^ wont be found
		}
	);
});

```

Basically, to keep the meaning of `this` you should use arrow functions, which you really should anyway in maybe 99.99% of cases 😛

**Edit:** So this is how it should look

```auto
serverIntervention.forEach(row => { // <------ This
	this.interventionService.getById(row.int_id).then(
		result => {
			console.log("int exist "+this.commonService.stringfyJson(result));
		},
		error => {
			console.log("int not exist "+this.commonService.stringfyJson(error));
		}
	);
});

```
