# Elasticsearch.js \_ search API error body is not defined

**URL:** <https://discuss.elastic.co/t/elasticsearch-js-search-api-error-body-is-not-defined/167147>\
**Category:** Kibana\
**Created:** [February 5, 2019, 3:16pm UTC](https://discuss.elastic.co/t/elasticsearch-js-search-api-error-body-is-not-defined/167147 "2019-02-05T15:16:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![adityaPsl](https://avatars.discourse-cdn.com/v4/letter/a/e47c2d/32.png) [@adityaPsl](https://discuss.elastic.co/u/adityaPsl)\
**Post date:** [February 5, 2019, 3:16pm UTC](https://discuss.elastic.co/t/elasticsearch-js-search-api-error-body-is-not-defined/167147/1 "2019-02-05T15:16:02Z")

</div>

Hello Team,

I am have created a custom plugin and i am trying to call elasticsearch \_ search API using elasticsearch.js  
I have created custom route

```
server.route({
    path: '/api/HelloWorld/search_test',
    method: 'POST',
    handler(req, reply) {
      const query = req.payload.searchterm;
	  client.search({
		index: 'inspections',
		body: {
			query: {
				match: {
					"name": query
				}
			}
		}
	})

```

when the function executes , it hits elastic search API and following API is shown in logs.

```
starting request {
  "method": "POST",
  "path": "/inspections/_search",
  "body": {
    "query": {
      "match": {
        "name": "1"
      }
    }
  },
  "query": {}
} 

```

and in response it gives correct response

However, logs shows following error

`error [14:57:39.268] [warning][process] UnhandledPromiseRejectionWarning: ReferenceError: body is not defined`

Could you please let me know if i am missing something.

Thank you very much for your help and support.

Thank you,  
Aditya

---

<div class="post-metadata">

**Author:** ![Nathan\_Reese](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nathan_reese/32/84829_2.png) [@Nathan\_Reese](https://discuss.elastic.co/u/Nathan_Reese)\
**Post date:** [February 5, 2019, 7:33pm UTC](https://discuss.elastic.co/t/elasticsearch-js-search-api-error-body-is-not-defined/167147/2 "2019-02-05T19:33:08Z")

</div>

You will want to access the elasticsearch cluster in this way to ensure elasticsearch is called with the correct permissions

```auto
const server = request.server;
const { callWithRequest } = server.plugins.elasticsearch.getCluster('data');
const searchPayload = {
  index: 'myIndex',
  body: {}
}
const response = await callWithRequest(request, 'search', searchPayload);

```

callWithRequest is a wrapper around [javascript elasticsearch client](https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference.html)

Your sample code is missing how reply is called so I am not sure where the promise error is coming from

---

<div class="post-metadata">

**Author:** ![adityaPsl](https://avatars.discourse-cdn.com/v4/letter/a/e47c2d/32.png) [@adityaPsl](https://discuss.elastic.co/u/adityaPsl)\
**Post date:** [February 8, 2019, 3:39am UTC](https://discuss.elastic.co/t/elasticsearch-js-search-api-error-body-is-not-defined/167147/3 "2019-02-08T03:39:10Z")

</div>

Thank you very much Nathan\_Reese.

I am able to use callWithRequest and get the data from the elasticsearch.

```
handler(req, reply) {
const query_param = req.payload.searchterm;	
const { callWithRequest } = server.plugins.elasticsearch.getCluster('data');
const searchPayload = {
	index: 'inspections',
body: {
	query: {
			match: {
				"name": query_param
			}
		}
	
	}
}
callWithRequest(req, 'search', searchPayload).then(response => {
	console.log(response.status);
	console.log({response});
	console.log(response);
	})

}

```

Just one query , One point i did not understand, when i hit api 'localhost:9200' from browser i get cluster name as 'elasticsearch' but when i use that name to get callWithRequest

```
{
  "name" : "Z3QtD8T",
  "cluster_name" : " **elasticsearch**",
  "cluster_uuid" : "1Z3o3i2WQ6eiH6Ayi2Rhiw",
  "version" : {
    "number" : "6.5.1",
    "build_flavor" : "default",
    "build_type" : "zip",
    "build_hash" : "8c58350",
    "build_date" : "2018-11-16T02:22:42.182257Z",
    "build_snapshot" : false,
    "lucene_version" : "7.5.0",
    "minimum_wire_compatibility_version" : "5.6.0",
    "minimum_index_compatibility_version" : "5.0.0"
  },
  "tagline" : "You Know, for Search"
}

```

`server.plugins.elasticsearch.getCluster('elasticsearch');`  
It gives a Type Error but when i changed that name to 'data' , it worked.

Thanks agian for your help and support.

Thank you,  
Aditya

---

<div class="post-metadata">

**Author:** ![Nathan\_Reese](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nathan_reese/32/84829_2.png) [@Nathan\_Reese](https://discuss.elastic.co/u/Nathan_Reese)\
**Post date:** [February 8, 2019, 4:02am UTC](https://discuss.elastic.co/t/elasticsearch-js-search-api-error-body-is-not-defined/167147/4 "2019-02-08T04:02:12Z")

</div>

`getCluster` accepts either `data` or `admin`. The value is not related to the name of your cluster. Its kibana's way of differentiating between the elasticsearch instance holding data and the elasticsearch instance holding admin stuff like saved objects

---

<div class="post-metadata">

**Author:** ![adityaPsl](https://avatars.discourse-cdn.com/v4/letter/a/e47c2d/32.png) [@adityaPsl](https://discuss.elastic.co/u/adityaPsl)\
**Post date:** [February 11, 2019, 5:45am UTC](https://discuss.elastic.co/t/elasticsearch-js-search-api-error-body-is-not-defined/167147/5 "2019-02-11T05:45:28Z")

</div>

Ok.. Thank you very much Nathan\_ Riese.  
Really appreciate your help.

Thank you  
Aditya

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [March 11, 2019, 5:45am UTC](https://discuss.elastic.co/t/elasticsearch-js-search-api-error-body-is-not-defined/167147/6 "2019-03-11T05:45:30Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
