Search code examples
javascriptentitymarklogic

marklogic create instance error out on array - javascript


[javascript] XDMP-CAST: (err:FORG0001) xs:string#1(.) -- Invalid cast: `json:object(<json:object xmlns:xs="http://www.w3.org/2001/XMLSchema" `
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" .../>) cast as xs:string
Stack Trace
In /MarkLogic/entity-services/entity-services.xqy on line 313
In xs:string#1(.)
fn:QName("http://marklogic.com/entity-services","source-nodes") = 
fn:doc("/budget/Accomplishments/objective/2019-6.json")/array-node("tasks")/object-node()
fn:QName("http://marklogic.com/entity-services","fn") = xs:string#1`

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

I am trying to create instance documents but I error out on line

let $tasks  := es:extract-array($source-node/tasks, xs:string#1) 

I am not sure why this errors out

Sample

{

"goal": "- Launched", 
 "tasks": [
 {
 "Task-1": "-- Step 1"
 }, 
 {
 "Task-2": "-- Step 22"
 }, 
 {
 "Task-3": "-- Step 33"
 }
 ], 
 "id": "1"
 }

Here is more code about the task

I have tire server alternatives to the code below. It builds and deplys the instance but there are no tasks in the array


Solution

  • MarkLogic will walk and work your tree like:

    /array-node()/object-node()/array-node()/text()[#], …
    

    Below customised module converts the array into canonical model using MarkLogic Entity Services:

    Within the instance data extract function, please replace your code

    let $tasks  := es:extract-array($source-node/tasks, xs:string#1)
    

    with my codes (works for 0..1, 1..1, 1..*)

    ==================================================================
        let $Tasks :=   $source-node/tasks[1 to fn:last()]
    ==================================================================
            =>es:optional('Tasks', array-node{$Tasks})
    };
    
    

    harmonised and enveloped document

    {
      "envelope": {
        "instance": {
         "info": {
           "title": "CompletedObjective", 
           "version": "1.0"
         }, 
         "CompletedObjective": {
           "objectiveID": "1", 
           "objectiveGoal": "- Launched", 
           "Tasks": [
             {
              "Task-1": "-- Step 1"
             }, 
             {
              "Task-2": "-- Step 22"
             }, 
             {
              "Task-3": "-- Step 33"
             }
           ]
         }
       }, 
       "attachments": [
         {
          "goal": "- Launched", 
          "tasks": 
          [
    =======================
    

    NOTE: You can also leverage MarkLogic Data Hub Framework to facilitate data modelling.