# TypeError: Cannot read property 'findEnemies' of undefined

**URL:** <https://discourse.codecombat.com/t/typeerror-cannot-read-property-findenemies-of-undefined/22039>\
**Category:** Level Help\
**Created:** [June 5, 2020, 2:31am UTC](https://discourse.codecombat.com/t/typeerror-cannot-read-property-findenemies-of-undefined/22039 "2020-06-05T02:31:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bubble\_Animations](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/bubble_animations/32/11708_2.png) [@Bubble\_Animations](https://discourse.codecombat.com/u/Bubble_Animations)\
**Post date:** [June 5, 2020, 2:31am UTC](https://discourse.codecombat.com/t/typeerror-cannot-read-property-findenemies-of-undefined/22039/1 "2020-06-05T02:31:47Z")

</div>

Hello!

My soldiers are unable to find enemies. Here is part of my code:

```python
var friends = hero.findFriends();
var person = friends[0];
var enemies = person.findEnemies();

```

The program highlights the third line (`var enemies`) and sends me the message  
 ![image](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/3/371e8ee7b3416b7e7c91191142810ff80618a4e1.png)  
I have checked over my code and I’m certain I have no typos.

I am playing the level Kelvintaph Crusader. If you need the rest of the code, let me know!

Thank you in advance ❤

---

<div class="post-metadata">

**Author:** ![Bubble\_Animations](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/bubble_animations/32/11708_2.png) [@Bubble\_Animations](https://discourse.codecombat.com/u/Bubble_Animations)\
**Post date:** [June 5, 2020, 2:37am UTC](https://discourse.codecombat.com/t/typeerror-cannot-read-property-findenemies-of-undefined/22039/2 "2020-06-05T02:37:41Z")

</div>

Also, when I change the code to:

```python
var friends = hero.findFriends();
var person = friends[0];
var enemies = person.findByType("witch")[0];

```

Then it gives me a similar error message:  
 ![image](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/b/bf1e2fb44a2a726fd225d2ea92fae249637b50c1.png)

---

<div class="post-metadata">

**Author:** ![Valentino\_Artawan](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/valentino_artawan/32/12153_2.png) [@Valentino\_Artawan](https://discourse.codecombat.com/u/Valentino_Artawan)\
**Post date:** [June 5, 2020, 2:47am UTC](https://discourse.codecombat.com/t/typeerror-cannot-read-property-findenemies-of-undefined/22039/3 "2020-06-05T02:47:34Z")

</div>

you never specified if there is actually a friend or not. use this line:

```python
if person
*your code here*

```

this also happens when i check enemy distances. you have to make sure that the target exists

Hope this helps  
Riley

---

<div class="post-metadata">

**Author:** ![Bubble\_Animations](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/bubble_animations/32/11708_2.png) [@Bubble\_Animations](https://discourse.codecombat.com/u/Bubble_Animations)\
**Post date:** [June 5, 2020, 2:54am UTC](https://discourse.codecombat.com/t/typeerror-cannot-read-property-findenemies-of-undefined/22039/4 "2020-06-05T02:54:19Z")

</div>

Thank you for replying!

I tried adding an if statement, and it still gives me an error message. Here’s a picture:

 ![image](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/1/1c4d36de9e914fa8c328d291d5cd0c5310131b3c.png)

---

<div class="post-metadata">

**Author:** ![Valentino\_Artawan](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/valentino_artawan/32/12153_2.png) [@Valentino\_Artawan](https://discourse.codecombat.com/u/Valentino_Artawan)\
**Post date:** [June 5, 2020, 2:57am UTC](https://discourse.codecombat.com/t/typeerror-cannot-read-property-findenemies-of-undefined/22039/5 "2020-06-05T02:57:55Z")

</div>

oh that… for findByType it can only be used on the hero. other friends dont know that method and therefore cannot use it. since the findByType method returns an array, you only need to use `person.findnearest(hero.findByType(type))`. which acts as a way to find the nearest enemy of that type

---

<div class="post-metadata">

**Author:** ![Bubble\_Animations](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/bubble_animations/32/11708_2.png) [@Bubble\_Animations](https://discourse.codecombat.com/u/Bubble_Animations)\
**Post date:** [June 5, 2020, 3:08am UTC](https://discourse.codecombat.com/t/typeerror-cannot-read-property-findenemies-of-undefined/22039/6 "2020-06-05T03:08:01Z")

</div>

Ohhhh, thank you!! I get it now 😄

---

<div class="post-metadata">

**Author:** ![Valentino\_Artawan](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/valentino_artawan/32/12153_2.png) [@Valentino\_Artawan](https://discourse.codecombat.com/u/Valentino_Artawan)\
**Post date:** [June 5, 2020, 12:42pm UTC](https://discourse.codecombat.com/t/typeerror-cannot-read-property-findenemies-of-undefined/22039/7 "2020-06-05T12:42:18Z")

</div>

Happy to help 🙂
