steve
steve

Reputation: 443

MarkLogic JavaScript Issue

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');

?

Upvotes: 1

Views: 142

Answers (1)

James Kerr
James Kerr

Reputation: 506

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.

Upvotes: 2

Related Questions