I am new to Elastic search and node js. I have configured ELK in my PC successfully. I have done basic queries using dev tools in kibana. Also I invoked search method from postman. Everything is working fine. But when I am trying to integrate elastic search in nodejs application, the request to elastic search is getting failed and nothing is returned.
Here is small snippet
const fetchElasticData = async () => {
try {
if (client.transport.connectionPool) {
console.log("Elasticsearch client is connected.");
} else {
console.log("Elasticsearch client is not connected.");
}
const { body: response } = await client.search({
index: "mongodb-data",
body: {
query: {
match_all: {},
},
},
});
console.log("Response :" + response);
console.log(response.hits.hits);
} catch (error) {
console.error("Error searching Elasticsearch:", error);
}
};
fetchElasticData()
When I am trying to run this, I am getting output as follows:
G:server> npm start
> [email protected] start
> node server.js
Elasticsearch client is connected.
Response :undefined
Error searching Elasticsearch: TypeError: Cannot read properties of undefined (reading 'hits')
at fetchElasticData1 (G:servercontrollerssms_Controller.js:237:26)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
I want to know where I am missing. Your help is highly appreciated. Thanks in advance!!