1

Trying to carefully work through this exercise in Entity Services at https://docs.marklogic.com/9.0/guide/entity-services/getting-started#id_75988 Ive been following the instructions to the letter and so far so good, until now. MarkLogic server version is 9.0-8.2

When I try and run the code ( below ) located in the section "Query the Data" :

'use strict';
import jsearch from '/MarkLogic/jsearch.mjs';

// Find all occurences of lastName with the value 'washington' contained
// in an es:instance element. Return just the documents in the results.
const people = jsearch.collections('person-envelopes');
const matches = people.documents()
  .where(cts.elementQuery(
      fn.QName('http://marklogic.com/entity-services', 'instance'),
      cts.elementValueQuery('lastName', 'washington')))
  .map(match => match.document)
  .result();

it errors with :

[javascript] JS-JAVASCRIPT: import jsearch from ('/MarkLogic/jsearch'); -- Error running JavaScript 
request: SyntaxError: Unexpected token import
Stack Trace
At line 2 column 21:
In import jsearch from ('/MarkLogic/jsearch');

1. 'use strict';
2. import jsearch from ('/MarkLogic/jsearch');
3. // Find all occurences of lastName with the value 'washington' contained
4. // in an es:instance element. Return just the documents in the results.

Can someone please suggest a work around or a solution please? Ive spent a while trying to find solutions but no joy. Not sure if web browser ( recent version of Brave browser ) is part of the issue.

Maybe replace

import jsearch from '/MarkLogic/jsearch.mjs';

with

const jsearch = require('/MarkLogic/jsearch.sjs');

?

Mads Hansen
  • 63,927
  • 12
  • 112
  • 147
steve
  • 395
  • 2
  • 11

1 Answers1

2

MarkLogic 10 introduced support for JavaScript modules (*.mjs), so it looks like this is an example that is shared between versions in the docs but it shouldn't be.

Your guess to change it to

const jsearch = require('/MarkLogic/jsearch.sjs');

looks correct. See https://docs.marklogic.com/9.0/guide/search-dev/javascript#id_48662 for examples using jsearch.

Mads Hansen
  • 63,927
  • 12
  • 112
  • 147
James Kerr
  • 506
  • 2
  • 4
  • OK, thank you. This question came about working through one example of building a simple entity model inside a data hub. Are there other worked examples ( outside of an MarkLogic training course) that cover building a simple entity model inside a data hub, please? I like to try and work things through myself before asking for further guidance. – steve Jan 18 '21 at 00:05