sudo-suhas/elastic-builder

TypeError: Cannot convert undefined or null to object

Opened this issue · 2 comments

Hi suhas,

Is it possible to refactor some of the query building logic into modules? I want to separate parts of the process into nodejs modules. but when I do this I get the title error.

I posted the full question here

Perhaps it's not possible?

Like this:

import esb from 'elastic-builder';
import { ToElasticFilter } from 'filtermodule';
import { ToElasticSort } from 'sortmodule';

const run = () => {

  // add any customers filters here
  const boolBody = ToElasticFilter(); // esb.boolQuery().must(esb.regexpQuery('catch_all', `.*test.*`).flags('NONE'));

  // apply the sorting here
  const s = ToElasticSort(); // esb.sort('catch_all', 'asc').missing('_last').mode('max').unmappedType('keyword');

  // build the base query here
  const body = esb.requestBodySearch().query(boolBody).sort(s);

  console.log(JSON.stringify(body.toJSON(), null, 2));

};

run();

Thank you for your time and work.

Could you share the stack trace? It is not clear which step is failing. It is possible that there is an issue with the import since you are not using relative paths

Stacktrace:

C:\Program Files\nodejs\node.exe .\dist\index.js
Uncaught TypeError TypeError: Cannot convert undefined or null to object
    at formatProperty (c:\Users\d.ma\Projects\github\esb_modules_sample\service\node_modules\elastic-builder\src\core\inspect.js:289:10)
    at <anonymous> (c:\Users\d.ma\Projects\github\esb_modules_sample\service\node_modules\elastic-builder\src\core\inspect.js:169:13)
    at formatValue (c:\Users\d.ma\Projects\github\esb_modules_sample\service\node_modules\elastic-builder\src\core\inspect.js:168:23)
    at inspect (c:\Users\d.ma\Projects\github\esb_modules_sample\service\node_modules\elastic-builder\src\core\inspect.js:39:12)
    at checkType (c:\Users\d.ma\Projects\github\esb_modules_sample\service\node_modules\elastic-builder\src\core\util.js:28:20)
    at query (c:\Users\d.ma\Projects\github\esb_modules_sample\service\node_modules\elastic-builder\src\core\request-body-search.js:84:9)
    at run (c:\Users\d.ma\Projects\github\esb_modules_sample\service\src\index.ts:14:40)
    at <anonymous> (c:\Users\d.ma\Projects\github\esb_modules_sample\service\src\index.ts:20:1)
    at Module._compile (internal/modules/cjs/loader:1256:14)
    at Module._extensions..js (internal/modules/cjs/loader:1310:10)
    at Module.load (internal/modules/cjs/loader:1119:32)
    at Module._load (internal/modules/cjs/loader:960:12)
    at executeUserEntryPoint (internal/modules/run_main:86:12)
    at <anonymous> (internal/main/run_main_module:23:47)
Process exited with code 1

It seems that, because the esb instance is different within the module, compared to the instance used within the service, then this error occurs.

Test project here