Kibana route not registered in plugin API

I'm currently developing a plugin for Kibana (7.0, es 7.0, win10), but I'm having an unexpected issue. The first two routes I created worked fine (under server/routes/routing.js) but for some reason the third one won't be created, and no log would show up in console even though I call console.log().

Here is my code:

  async function getStatuses(server){
      let promise = fetch("http://es_instance:port/index/_search",{
        method: "GET",
        mode: "cors",
        headers: {"Content-Type": "application/json"},
        body: {
          "query": { "match_all": {} },
          "aggs": {
            "max_status": {
              "max": {
                "field": "global_status",
                "missing": 0
              }
            }
          }
        }
      }).then(res=>res.json()).then(body=>{
        console.log(body);
        let a = body.hits.hits;
        return { global_status: body.aggregations.max_status.value,
          last_message: a[a.length-1] };
      });

      let res = await promise;

      console.log(res);
      server.route({
        path: '/api/my_plugin_name/my_route',
        method: 'GET',
        handler() {
          return { statuses: JSON.stringify(res) };
        }
      });
}

My function is called there:

export default function (server) {
    [...]//registering routes in API
    getStatuses(server);
}

When I'm hitting directly /api/my_plugin_name/my_route, I get a 404. Is it possible that registering routes can somehow return (stop function execution) before getStatuses() is even called? Or am I missing something?

Now I know for sure function is called. I did a GET instead of a POST. But anyway, route is still not registered. I'm having trouble parsing response of the request. Whatever the way I get the result (json(), text(), blob() or arrayBuffer()), when I parse it and log it, it says

found START_ARRAY instead of START_OBJECT

and I can't manage to find why. I'd be glad if someone could help.

For reference, when I get it as a blob, I see this:

Blob {
[Symbol(type)]: 'application/json; charset=utf-8',
[Symbol(buffer)]:
<Buffer 7b 22 65 72 72 6f 72 22 3a 7b 22 72 6f 6f 74 5f 63 61 75 73 65 22 3a 5b 7b 22 74 79 70 65 22 3a 22 70 61 72 73 69 6e 67 5f 65 78 63 65 70 74 69 6f 6e ... >
}

so json() should work well. And it's not an array but a simple json, I know it because I tested the exact same request with postman.

Where is this being called from? This should be in the init function of your plugin.

Yes it is
in init() function in index.js precisely

Ok, I managed to solve it. For those who got here, my mistake was to put the body straight in json in the fetch. I should have done it like that:

  body: JSON.stringify({
    "query": { "match_all": {} },
    "aggs": {
      "max_status": {
        "max": {
          "field": "global_status",
          "missing": 0
        }
      }
    }
  })

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