# Custom visualization request handler cannot read Timefilter?

**URL:** <https://discuss.elastic.co/t/custom-visualization-request-handler-cannot-read-timefilter/180789>\
**Category:** Kibana\
**Created:** [May 13, 2019, 10:47am UTC](https://discuss.elastic.co/t/custom-visualization-request-handler-cannot-read-timefilter/180789 "2019-05-13T10:47:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![reillye](https://avatars.discourse-cdn.com/v4/letter/r/c6cbf5/32.png) [@reillye](https://discuss.elastic.co/u/reillye)\
**Post date:** [May 13, 2019, 10:47am UTC](https://discuss.elastic.co/t/custom-visualization-request-handler-cannot-read-timefilter/180789/1 "2019-05-13T10:47:03Z")

</div>

I'm trying to write a custom request handler for retrieving the index source from ES. I have found an example from this plugin [here](https://github.com/ReneU/kibana-d3-sankey/blob/master/public/RequestHandlerProvider.js) that I am trying to copy. I have tried cloning this plugin and running it in order to debug it and understand how it works, but when I try to create the visualisation I get the following error:

`TypeError: Cannot read property 'from' of undefined`

on this line: `'gte': timeFilter.from,`

I am trying to run this under kibana `6.5.4`, but I am unsure if this plugin may be outdated and simply not using the correct attributes anymore. Can anyone help with this error? This full source code is below:

```
const getRequestBody = (params, queryFilter, timeFilter) => {
  const requestBody = {
    'size': 0,
    'query': {
      'bool': {
        'must': [
          {
            'range': {
              'timestamp': {
                'gte': timeFilter.from,
                'lte': timeFilter.to
              }
            }
          }
        ]
      }
    },
    'aggs': {
      'sessions': {
        'terms': {
          'field': params.sessionField,
          'size': params.maxSessionCount
        },
        'aggs': {
          'first_events': {
            'top_hits': {
              'sort': [
                {
                  [params.timeField]: {
                    'order': 'asc'
                  }
                }
              ],
              '_source': {
                'includes': [params.actionField]
              },
              'size': params.maxSessionLength
            }
          }
        }
      }
    }
  };
  const queries = queryFilter.getFilters();
  if (queries && queries.length) {
    queries.forEach(({ meta }) => {
      if (meta.disabled) return;
      let query;
      switch (meta.type) {
        case 'phrase':
          query = {
            match: {
              [meta.key]: meta.value
            }
          };
          addMustQuery(requestBody, query, meta);
          break;
        case 'phrases':
          meta.params.forEach(param => {
            query = {
              match: {
                [meta.key]: param
              }
            };
            addShouldQuery(requestBody, query, meta);
          });
          break;
        case 'range':
          query = {
            range: {
              [meta.key]: meta.params
            }
          };
          addRangeQuery(requestBody, query, meta);
          break;
        case 'exists':
          query = {
            exists: {
              field: meta.key
            }
          };
          addMustQuery(requestBody, query, meta);
      }
    });
  }
  return requestBody;
};

function addMustQuery(request, query, { negate }) {
  const boolObject = request.query.bool;
  let matcher;
  if (negate) {
    matcher = boolObject.must_not ? boolObject.must_not : (boolObject.must_not = []);
  } else {
    matcher = boolObject.must ? boolObject.must : (boolObject.must = []);
  }
  matcher.push(query);
}

function addShouldQuery(request, query, { negate }) {
  let matcher;
  if (negate) {
    matcher = request.query.bool.must_not ? request.query.bool.must_not : (request.query.bool.must_not = []);
  } else {
    matcher = request.query.bool.should ? request.query.bool.should : (request.query.bool.should = []);
    request.query.bool.minimum_should_match = 1;
  }
  matcher.push(query);
}

function addRangeQuery(request, query, { negate }) {
  let matcher;
  if (negate) {
    matcher = request.query.bool.must_not ? request.query.bool.must_not : (request.query.bool.must_not = []);
  } else {
    matcher = request.query.bool.must;
  }
  matcher.push(query);
}

export function RequestHandlerProvider(Private, es) {
  return {
    handle(vis) {
      const { timeFilter, queryFilter } = vis.API;
      return new Promise(resolve => {
        const params = vis.params;
        const requestBody = getRequestBody(params, queryFilter, timeFilter.time);
        es.search({
          index: 'analytics',
          body: requestBody
        }).then(result => resolve(result));
      });
    }
  };
}
```

---

<div class="post-metadata">

**Author:** ![reillye](https://avatars.discourse-cdn.com/v4/letter/r/c6cbf5/32.png) [@reillye](https://discuss.elastic.co/u/reillye)\
**Post date:** [May 14, 2019, 9:02am UTC](https://discuss.elastic.co/t/custom-visualization-request-handler-cannot-read-timefilter/180789/2 "2019-05-14T09:02:37Z")

</div>

I have found [this blog post](https://www.elastic.co/blog/upcoming-kibana-plugin-api-changes-in-6-4) that says access to Timefilter was removed as of Kibana 6.4, but the page doesn't give any good examples of how to access now, at least to someone who is unfamiliar with it.

I tried accessing the `from` and `to` values using `timeFilter.time.to` etc. but this also fails with the same error as above

---

<div class="post-metadata">

**Author:** ![reillye](https://avatars.discourse-cdn.com/v4/letter/r/c6cbf5/32.png) [@reillye](https://discuss.elastic.co/u/reillye)\
**Post date:** [May 14, 2019, 10:49am UTC](https://discuss.elastic.co/t/custom-visualization-request-handler-cannot-read-timefilter/180789/3 "2019-05-14T10:49:15Z")

</div>

I found the solution to this. The issue was that the [Timefilter.js](https://github.com/elastic/kibana/blob/6.5/src/ui/public/timefilter/timefilter.js#L40) for `6.5` changed from using `time` to `_time`. This means that I had to update line `const requestBody = getRequestBody(params, queryFilter, timeFilter.time)` to be `const requestBody = getRequestBody(params, queryFilter, timeFilter._time)`, which resolved the issue.

---

<div class="post-metadata">

**Author:** ![tylersmalley](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tylersmalley/32/8833_2.png) [@tylersmalley](https://discuss.elastic.co/u/tylersmalley)\
**Post date:** [May 14, 2019, 3:00pm UTC](https://discuss.elastic.co/t/custom-visualization-request-handler-cannot-read-timefilter/180789/4 "2019-05-14T15:00:28Z")

</div>

Thanks for posting the solution!

---

<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:** [June 11, 2019, 3:00pm UTC](https://discuss.elastic.co/t/custom-visualization-request-handler-cannot-read-timefilter/180789/5 "2019-06-11T15:00:32Z")

</div>

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