1

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?

Mads Hansen
  • 63,927
  • 12
  • 112
  • 147
bdkdavid
  • 203
  • 3
  • 14
  • Is your collection really `Objective,Accomplishments`, or did you intend to specify an array of two collections: `fn.collection(['Objective', 'Accomplishments'])` – Mads Hansen Oct 24 '20 at 15:53
  • yes, I wanted two different collections, but it seens that I got one collection – bdkdavid Oct 26 '20 at 11:48
  • fn.collection('Objective', 'Accomplishments')) { -- Too many args, expected 1 but got 2 – bdkdavid Oct 27 '20 at 20:36
  • I recieve the above error – bdkdavid Oct 27 '20 at 20:36
  • you forgot the square braces, it takes an array as the parameter. Not `fn.collection('Objective', 'Accomplishments') ` but `fn.collection(['Objective', 'Accomplishments'])` – Mads Hansen Oct 27 '20 at 20:42

2 Answers2

2

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;
Mads Hansen
  • 63,927
  • 12
  • 112
  • 147
  • I just wanted to add that I used the es.instanceConverterGenerate to generate the instance converter module and that is the function name that it had provided – bdkdavid Oct 26 '20 at 11:52
  • Right, I missed that this was an XQuery Entity Services module that was being imported. Even though it is kebab-case in the XQuery module, when you use `require()` it should be visible as `extractInstanceCompletedObjectives()`. Can you try that? – Mads Hansen Oct 26 '20 at 15:52
  • Yes, I got the following result:extractInstanceCompletedObjectives – bdkdavid Oct 27 '20 at 20:08
  • I changed line 7 to the code you suggested: I get the following error: – bdkdavid Oct 27 '20 at 20:10
  • PlannedObjective.extractInstanceCompletedObjectives is not a function – bdkdavid Oct 27 '20 at 20:10
  • `PlannedObjective`? I though the object was `Completed`? – Mads Hansen Oct 27 '20 at 20:44
0

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

bdkdavid
  • 203
  • 3
  • 14