skip to Main Content

“The returned value of an API call will no longer be the body, statusCode, and headers for callbacks, and only the body for promises. The new returned value will be a unique object containing the body, statusCode, headers, warnings, and meta, for both callback and promises.”

This is a problem if I stringify and store the result in redis (now I will need to JSON parse when I get the value back from redis). Is there anyway I can switch on “body only” mode on promises?

2

Answers


  1. Unforntunately, no.
    There is no way to only receive only the body.
    As you mentioned this would be the response,

    {
      body: object | boolean
      statusCode: number
      headers: object
      warnings: [string]
      meta: object
    }
    

    Best you can do it before you store the response in Redis you can store it like this,

    const {body} = await client.search({
      index: 'my-index',
      body: { foo: 'bar' }
    })
    // Now you can store the body in Redis
    

    Or when you fetch the response object from Redis you can do this,

    const {body}=JSON.parse(fetchFromRedis(id));
    
    Login or Signup to reply.
  2. As a workaround you could overwrite/proxy the search() method of your Client-instance. So you could define your proxy with something like:

    function initializeSearchProxy(client) {
        const origSearch = client.search;
        client.search = async function () {
            const {body} = await origSearch.apply(this, arguments);
            return body;
        }
    }
    

    Then initialize your search-proxy after requiring and instantiating the elasticsearch-Client,

    const {Client} = require('@elastic/elasticsearch');
    const client = new Client({node: 'http://localhost:9200'});
    initializeSearchProxy(client);
    ...
    // all client.search calls now return the body directly
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search