Elastic search connection with strapi , Error retrieving energy consumption data: TypeError: Cannot read properties of undefined (reading 'hits')

Strapi Version: 4.10.1

  • Operating System: Ubuntu 22.04
  • Database: sqlite
  • Node Version: 18.14
  • NPM Version: 9.3.1

  • issue discreption: I am trying to connect elasticsearch with strapi , but i can not , i am having this error when i try to consult the page :
    this is the url of the page : http://localhost:1337/api/energy-consumption
    this is the error i am having : { "error": "Cannot read properties of undefined (reading 'hits')" }
    here is the blog post that i have followed : How to Set Up and Use Elasticsearch with Strapi , I tried to follow his logic of the implemntation for my project .
    here is My docker compose file :

services:
  # Elasticsearch service
  elasticsearch:
    # Use the official Elasticsearch image from Docker Hub
    image: docker.elastic.co/elasticsearch/elasticsearch:8.6.2
    container_name: elasticsearch
    # Set the Environment variables for Elasticsearch
    environment:
      # Set discovery type to single node
      - discovery.type=single-node
      - node.name=elasticsearch
      # Lock the process memory to prevent swapping
      - bootstrap.memory_lock=true
      # Set java memory heap size
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
      # Disable security features (otherwise it won't work)
      - xpack.security.enabled=false
    # Expose ports for Elasticsearch
    ports:
      - 9200:9200
      - 9300:9300
    # Set memory limits for Elasticsearch
    ulimits:
      memlock:
        soft: -1
        hard: -1
    # Health check for Elasticsearch (check the cluster health)
    healthcheck:
      test: ["CMD-SHELL", "curl --silent --fail localhost:9200/_cluster/health || exit 1"]
      interval: 30s
      timeout: 10s
      retries: 5
    # Set network for Elasticsearch
    networks:
      - default

  # Logstash service
  logstash:
    # Use the official (and latest)logstash image from Docker Hub
    image: docker.elastic.co/logstash/logstash:8.6.2
    container_name: logstash
    # Mount Logstash configuration file
    volumes:
      - ./logstash-config:/usr/share/logstash/pipeline
    # set the Environment variables for Logstash
    environment:
      # Set Elasticsearch hosts
      - "ES_HOSTS=http://elasticsearch:9200"
    # Expose ports for Logstash
    ports:
      - 5000:5000
    # Set network for Logstash
    networks:
      - default

here is my logstach config file :

# Logstash configuration file
input {
  # Listen for HTTP input on all network interfaces on port 5000
  http {
    host => "0.0.0.0"
    port => 5000
  }
}

filter {
  # Parse the incoming JSON messages
  json {
    source => "message"
  }
  # Remove the original message field
  mutate {
    remove_field => ["message"]
  }
  # Parse the createAt field as a date
  date {
    match => ["createAt", "ISO8601"]
    target => "createAt"
  }
}

output {
  # Send the modified data to Elasticsearch
  elasticsearch {
    # Set Elasticsearch hosts
    hosts => ["http://elasticsearch:9200"]
    # Set index name from incoming headers
    index => "index"
  }
}

here is the implemntation of my api end point :
controllers :


/**
 * A set of functions called "actions" for `energy`
 */

module.exports = {
  energyConsumption: async (ctx) => {
    try {
      console.log('Energy consumption action called');

      const data = await strapi.service('api::energy.energy').find();
      console.log('Retrieved energy consumption data:', data);

      ctx.send(data);
    } catch (err) {
      console.error('Error retrieving energy consumption:', err);

      ctx.send({ error: err.message }, 500);
    }
  }
};

here is the implemntnation of the routes file
Routes:

  routes: [
    {
      method: 'GET',
      path: '/energy-consumption',
      handler: 'energy.energyConsumption',
      config: {
        policies: [],
        middlewares: [],
      },
    },
    {
      method: 'POST',
      path: '/energy-consumption',
      handler: 'energy.energyConsumption',
      config: {
        policies: [],
        middlewares: [],
      },
    },
    // Add more custom routes as needed
  ],
};

and here is the implemntation of services file
Services :


const { createCoreService } = require('@strapi/strapi').factories;

module.exports = createCoreService('api::energy.energy', ({ strapi }) => ({
  async find(...args) {
    const { connector, testConn } = require('../../../../helpers/client_elastic.js');
    const client = connector();

    testConn(client);
    console.log('Connection to Elasticsearch successful');

    const searchParams = {
      index: 'index', // Replace with the appropriate index name
      body: {
        query: {
          match_all: {} // Retrieve all documents
        }
      }
    };

    try {
      console.log('Sending search request to Elasticsearch');
      const { body } = await client.search(searchParams);
      console.log('Received response from Elasticsearch:', JSON.stringify(body, null, 2));

      const hits = body.hits && body.hits.hits ? body.hits.hits : [];
      console.log('Total hits:', hits.length);

      const mappedData = hits.map((hit) => {
        const source = hit._source;
        return {
          name: source.name,
          measurePoint: source.measurePoint,
          current: source.current,
          voltage: source.voltage,
          activePower: source.activePower,
          cosphi: source.cosphi,
          powerFactor: source.powerFactor,
          date: source.date
        };
      });

      console.log('Transformed data:', mappedData);
      return mappedData;
    } catch (error) {
      console.error('Error retrieving energy consumption data:', error);
      throw error;
    }
  }
}));

here is the log of the server when i try to go to the pages url :
server log:

To manage your project 🚀, go to the administration panel at:
http://localhost:1337/admin

To access the server ⚡️, go to:
http://localhost:1337

Energy consumption action called
Connection to Elasticsearch successful
Sending search request to Elasticsearch
{
  name: 'elasticsearch',
  cluster_name: 'docker-cluster',
  cluster_uuid: '0vMVTgPxSnWS-qmeJoGj1A',
  version: {
    number: '8.6.2',
    build_flavor: 'default',
    build_type: 'docker',
    build_hash: '2d58d0f136141f03239816a4e360a8d17b6d8f29',
    build_date: '2023-02-13T09:35:20.314882762Z',
    build_snapshot: false,
    lucene_version: '9.4.2',
    minimum_wire_compatibility_version: '7.17.0',
    minimum_index_compatibility_version: '7.0.0'
  },
  tagline: 'You Know, for Search'
}
Received response from Elasticsearch: undefined
Error retrieving energy consumption data: TypeError: Cannot read properties of undefined (reading 'hits')
    at Object.find (/home/walid/Desktop/15-05-2023/test/factoorya_website/Backend/src/api/energy/services/energy.js:27:25)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async Object.energyConsumption (/home/walid/Desktop/15-05-2023/test/factoorya_website/Backend/src/api/energy/controllers/energy.js:12:20)
    at async returnBodyMiddleware (/home/walid/Desktop/15-05-2023/test/factoorya_website/Backend/node_modules/@strapi/strapi/lib/services/server/compose-endpoint.js:52:18)
    at async policiesMiddleware (/home/walid/Desktop/15-05-2023/test/factoorya_website/Backend/node_modules/@strapi/strapi/lib/services/server/policy.js:24:5)
    at async serve (/home/walid/Desktop/15-05-2023/test/factoorya_website/Backend/node_modules/koa-static/index.js:59:5)
    at async returnBodyMiddleware (/home/walid/Desktop/15-05-2023/test/factoorya_website/Backend/node_modules/@strapi/strapi/lib/services/server/compose-endpoint.js:52:18)
    at async policiesMiddleware (/home/walid/Desktop/15-05-2023/test/factoorya_website/Backend/node_modules/@strapi/strapi/lib/services/server/policy.js:24:5)
    at async /home/walid/Desktop/15-05-2023/test/factoorya_website/Backend/node_modules/@strapi/strapi/lib/middlewares/body.js:58:9
    at async /home/walid/Desktop/15-05-2023/test/factoorya_website/Backend/node_modules/@strapi/strapi/lib/middlewares/logger.js:9:5
Error retrieving energy consumption: TypeError: Cannot read properties of undefined (reading 'hits')
    at Object.find (/home/walid/Desktop/15-05-2023/test/factoorya_website/Backend/src/api/energy/services/energy.js:27:25)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async Object.energyConsumption (/home/walid/Desktop/15-05-2023/test/factoorya_website/Backend/src/api/energy/controllers/energy.js:12:20)
    at async returnBodyMiddleware (/home/walid/Desktop/15-05-2023/test/factoorya_website/Backend/node_modules/@strapi/strapi/lib/services/server/compose-endpoint.js:52:18)
    at async policiesMiddleware (/home/walid/Desktop/15-05-2023/test/factoorya_website/Backend/node_modules/@strapi/strapi/lib/services/server/policy.js:24:5)
    at async serve (/home/walid/Desktop/15-05-2023/test/factoorya_website/Backend/node_modules/koa-static/index.js:59:5)
    at async returnBodyMiddleware (/home/walid/Desktop/15-05-2023/test/factoorya_website/Backend/node_modules/@strapi/strapi/lib/services/server/compose-endpoint.js:52:18)
    at async policiesMiddleware (/home/walid/Desktop/15-05-2023/test/factoorya_website/Backend/node_modules/@strapi/strapi/lib/services/server/policy.js:24:5)
    at async /home/walid/Desktop/15-05-2023/test/factoorya_website/Backend/node_modules/@strapi/strapi/lib/middlewares/body.js:58:9
    at async /home/walid/Desktop/15-05-2023/test/factoorya_website/Backend/node_modules/@strapi/strapi/lib/middlewares/logger.js:9:5
[2023-05-17 16:08:14.917] http: GET /api/energy-consumption (235 ms) 500

this issue is basically here , and i don’t know , why is this happening , any help , tips is really apperciated