# I have some trouble with the findNearest() function called from an item

**URL:** https://discourse.codecombat.com/t/i-have-some-trouble-with-the-findnearest-function-called-from-an-item/2823
**Category:** Bugs
**Created:** [February 23, 2015, 10:48am UTC](https://discourse.codecombat.com/t/i-have-some-trouble-with-the-findnearest-function-called-from-an-item/2823 "2015-02-23T10:48:16Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![xZwop](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/xzwop/32/2092_2.png) [@xZwop](https://discourse.codecombat.com/u/xZwop)
#### Post date: [February 23, 2015, 10:48am UTC](https://discourse.codecombat.com/t/i-have-some-trouble-with-the-findnearest-function-called-from-an-item/2823/1 "2015-02-23T10:48:16Z")

</div>

Hello,

I’m was trying to play the multiplayer game Greed, and somehow I wanted to do this with my peasant:

- find out the closest gold to my peasant
- find out if there wasn’t an enemy peon closer than my peasant to the gold

But whenever I try to call findNearest from the item API, I have the following error poping up:

> Cannot read property ‘apply’ of undefined

I reduced the code to this, am I doing something wrong here or is this function bugged?

```
var base = this;
var items = base.getItems();

var item=base.getNearest(items);
if (item) {
    var closestEnemy = item.findNearest(base.getEnemies());
}

```

---

<div class="post-metadata">

### Author: ![J\_F\_B\_M](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/j_f_b_m/32/2756_2.png) [@J\_F\_B\_M](https://discourse.codecombat.com/u/J_F_B_M)
#### Post date: [February 23, 2015, 10:59am UTC](https://discourse.codecombat.com/t/i-have-some-trouble-with-the-findnearest-function-called-from-an-item/2823/2 "2015-02-23T10:59:55Z")

</div>

You are using one time `findNearest()` and one time `getNearest()`. I don’t recall right now which is the correct one, but I’m pretty sure only one of these will work.

---

<div class="post-metadata">

### Author: ![xZwop](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/xzwop/32/2092_2.png) [@xZwop](https://discourse.codecombat.com/u/xZwop)
#### Post date: [February 23, 2015, 11:02am UTC](https://discourse.codecombat.com/t/i-have-some-trouble-with-the-findnearest-function-called-from-an-item/2823/3 "2015-02-23T11:02:28Z")

</div>

base have on its API a getNearest() function, while item have in its API a findNearest() function.

![](https://sea2.discourse-cdn.com/flex016/uploads/codecombat/2093/1e6e2b2a9be32a14.png)  
 ![](https://sea2.discourse-cdn.com/flex016/uploads/codecombat/2095/5ee7e08eb89da930.png)

---

<div class="post-metadata">

### Author: ![J\_F\_B\_M](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/j_f_b_m/32/2756_2.png) [@J\_F\_B\_M](https://discourse.codecombat.com/u/J_F_B_M)
#### Post date: [February 23, 2015, 11:10am UTC](https://discourse.codecombat.com/t/i-have-some-trouble-with-the-findnearest-function-called-from-an-item/2823/4 "2015-02-23T11:10:01Z")

</div>

That’s funny, I never noticed this. I actually never had the idea to use an items findNearest-function. I usually try to read the target, or track enemies over several gameticks to guess where they are going. Yours is… so much faster.

Anyway, after I opened an issue about this: Did you try to swap either method-call with the equivalent other?  
Try to use `findNearest()` in both cases, or `getNearest()`. Maybe the info-popup is wrong.

---

<div class="post-metadata">

### Author: ![xZwop](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/xzwop/32/2092_2.png) [@xZwop](https://discourse.codecombat.com/u/xZwop)
#### Post date: [February 23, 2015, 11:18am UTC](https://discourse.codecombat.com/t/i-have-some-trouble-with-the-findnearest-function-called-from-an-item/2823/5 "2015-02-23T11:18:15Z")

</div>

Even if _**findNearest()**_ works as well as _**getNearest()**_ for the base object (I have no idea why there are two functions doing the same thing by the way), none of them works with the item object:

_**item.findNearest()**_ error is:

> Cannot read property ‘apply’ of undefined

_**item.getNearest()**_ error is:

> Undefined is not a function

---

<div class="post-metadata">

### Author: ![J\_F\_B\_M](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/j_f_b_m/32/2756_2.png) [@J\_F\_B\_M](https://discourse.codecombat.com/u/J_F_B_M)
#### Post date: [February 23, 2015, 12:10pm UTC](https://discourse.codecombat.com/t/i-have-some-trouble-with-the-findnearest-function-called-from-an-item/2823/6 "2015-02-23T12:10:26Z")

</div>

This issue is now tracked here:

> <https://github.com/codecombat/codecombat/issues/2381>

**Please post any new information in this issue rather than here, so the problem is handled centralized.**  
Thanks for your contribution.
