1
votes

error: [javascript] JS-JAVASCRIPT: let instance = Completed.extract-instance-CompletedObjectives(source); -- Error running JavaScript request: ReferenceError: instance is not defined:

My Code:

declareUpdate();

const es = require('/MarkLogic/entity-services/entity-services.xqy'); 
const Completed = require('/es-gs/CompletedObjectiveEntity-1.0.0-conv.xqy');
for (const source of fn.collection('Objective,Accomplishments')) {
let instance = Completed.extract-instance-CompletedObjectives(source);
let uri = '/es-gs/env/'+ instance.id + '.json';
xdmp.documentInsert(uri, Completed.instanceToEnvelope(instance, "json"),{collections ['CompletedObjective-envelopes']});}

Is in the modules database: es-gs/CompletedObjectiveEntity-1.0.0-conv.xqy

The output error is

Stack Trace At line 7 column 33: In let instance = Completed.extract-instance-CompletedObjectives(source);

  1. const Completed = require('/es-gs/CompletedObjectiveEntity-1.0.0-conv.xqy');
  2. for (const source of fn.collection('Objective,Accomplishments')) {
  3. let instance = Completed.extract-instance-CompletedObjectives(source);
  4. let uri = '/es-gs/env/'+ instance.id + '.json';
  5. xdmp.documentInsert(

The name of the function is declare function completedObjectives:extract-instance-CompletedObjectives()

I used the instance generator to create the module:

const es = require('/MarkLogic/entity-services/entity-services.xqy');
const ARTIFACT_DIR = '/data/modules/';
const desc = cts.doc('/es-gs/models/CompletedObjective.entity.json');
xdmp.save(ARTIFACT_DIR + 'CompletedObjectiveEntity-1.0.0-conv.xqy', es.instanceConverterGenerate(desc));

Can anyone point me in the right direction?

2
Is your collection really Objective,Accomplishments, or did you intend to specify an array of two collections: fn.collection(['Objective', 'Accomplishments'])Mads Hansen
yes, I wanted two different collections, but it seens that I got one collectionbdkdavid
fn.collection('Objective', 'Accomplishments')) { -- Too many args, expected 1 but got 2bdkdavid
I recieve the above errorbdkdavid
you forgot the square braces, it takes an array as the parameter. Not fn.collection('Objective', 'Accomplishments') but fn.collection(['Objective', 'Accomplishments'])Mads Hansen

2 Answers

2
votes

Change line 7 of your code to:

let instance = Completed.extractInstanceCompletedObjectives(source);

When you import the Entity Services XQuery module in a JavaScript module, even though the XQuery methods have kebab-case in the source, they will be exposed and available using camelCase names:

https://docs.marklogic.com/guide/entity-services/getting-started#id_pgfId-1117445

Invoke the functions using their JavaScript-style, camel-case names. For example, in the case of the Person entity type, the module converter functions can be invoked from Server-Side JavaScript using the following names, assuming the module is represented by a variable named person, as shown in the above require statement.

  • person.extractInstancePerson
  • person.instanceToEnvelope
  • person.instanceToCanonical

You can verify the method names exposed with camelCase by executing the following:

const Completed = require('/es-gs/CompletedObjectiveEntity-1.0.0-conv.xqy');
let functionNames = [];
for (const property in Completed) {
  functionNames .push(property);
}
functionNames;
0
votes

I changed line 7 in the original code above to:`let instance = Completed'extract-instance-CompletedObjectives';``