JavaScript encounters an error when trying to create a MarkLogic instance with an array

[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 struggling to generate instance documents due to an error occurring on a specific line.

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

I don't understand why this error is happening

Sample

{

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

Below is additional code related to task management

I have tried different server options for the code provided below. It successfully builds and deploys the instance, but the tasks within the array are missing

Answer №1

MarkLogic will navigate and process your tree structure as follows:

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

The specialized module below transforms the array into a standard model using MarkLogic Entity Services:

When implementing the instance data extraction function, kindly substitute your existing code

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

replace my implementation (compatible with 0..1, 1..1, 1..*)

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

structured and encapsulated 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": 
      [
=======================

Please note: The MarkLogic Data Hub Framework can also be utilized for enhanced data modeling.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Enhance your viewing experience by magnifying a specific element, all while maintaining full control to navigate

Is it possible to create a zoom effect on a webpage that focuses on one specific element while still allowing for navigation and scrolling? I have searched online for plugins like Fancybox and Zoomooz, but none of them offer the functionality I need. I sp ...

Various concatenated and compressed JavaScript files across multiple HTML documents within a single application

In my express.js application, I have different routes such as /home and /dashboard. For example, on the home page, I include: jquery.js, underscore.js, somemodule1.js, somemodule2.js. On the dashboard, I include: jquery.js, underscore.js, somemodule3.js, ...

What is the process for transmitting images from React Native to native modules?

Challenge I am facing an issue trying to send an array of locally saved images from the JavaScript side (stored in an assets folder) to both iOS and Android native sides. The native code processes the images and returns a new image successfully when using ...

Tips for personalizing text and icon colors in the TableSortText element of Material-ui

My Goal: I aim to empower users with the ability to apply customized styles to my EnhancedTable component by utilizing a styles object containing properties like headCellColor, headCellBackgroundColor, bodyCellColor, bodyCellBackgroundColor, and more. The ...

Utilizing *ngfor in Angular 7 to Group and Display Data

I currently have incoming data structured like this in my Angular application Visit this link to see the data format https://i.stack.imgur.com/jgEIw.png What is the best way to group and sort the data by State and County, and present it in a table as sh ...

Is it possible to dynamically change the port for an Express server?

Here is a common question that often arises among beginners, as I had the same query when I first started Is there a way to set the port for express without manually coding it or selecting a port yourself? This was something that puzzled me during my init ...

Developing a robust system for managing classnames

I have a question regarding the article I found on Quora about Facebook's CSS class names structure. They mentioned that both Facebook and Google use a build system to convert developer-friendly class names into shorter ones to save bandwidth and crea ...

Graph only displays data after button is clicked

I have been working on customizing this jsfiddle to display database data. Check it out here: http://jsfiddle.net/jlbriggs/7ntyzo6u/ Using JSON, I am fetching data from my database and editing chart1 to display the database data: var chart, chartOption ...

Using ThreeJs to create interactive 3D objects with button-controlled movement

Currently, I'm diving into the world of Three.js and I have a fascinating project in mind. I want to create movement buttons that will control the position of a sphere object. Through some research, I found out that I can use the onclick function on b ...

Using Thymeleaf in a Spring Boot application, the modal automatically closes after submission

How can I keep the modal open when I click the submit button? I want to reload an existing modal form when I click submit. The code form is in fragment/header, and when I click the "login" button, the modal shows up. The issue is that in views/loginForm, ...

Looking to encode/decode a JSON response in order to transfer it to a different webpage

Currently, I have a website application where I am required to pass a JSON response (in string format) across the site. To achieve this, I have been using a hidden type value and passing it upon the submission of a link/button which subsequently triggers a ...

What is the purpose of defining the initialState in Redux?

As per the Redux documentation, it is considered a best practice to establish an initialState for your reducer. However, maintaining this initialState can become challenging when the state relies on data from an API response, leading to discrepancies betwe ...

distance between the top of the window and divs within an array

I'm having trouble calculating the distance of divs within an array from the top of the window. Whenever I try to run the code, I keep getting an error message saying "player.offset is not a function". Can someone please point out where I am making a ...

How to conditionally set the active class in a React component

I am new to using React and I am encountering some issues. My goal is to add an active class when any of these components are opened. I have attempted a solution but it isn't working as expected. I need assistance in adding a background color to the d ...

Removing nested divs using JavaScript

My website has a nested div structure which contains multiple child divs. Here is an example of the div structure: <div id="outside-one"> <div class="inside" id="1"></div> <div class="inside" id="2"></div> <div ...

"Using Jest to specifically test the functionality of returning strings within an object

Attempting to run a jest test, it seemed like the issue was with the expect, toBe section as I believed that the two objects being compared (data, geonames) were exactly the same. However, upon closer inspection, they turned out to be different. The struct ...

Execute my function when the Redux state changes in the store in ReactJS

I am facing a specific issue in my React-Redux application. I am using Redux-saga to communicate with a REST API, which does not return a promise. Within my application, I also utilize state (in addition to the store) that I wish to update after receiving ...

I'm encountering an issue where my information doesn't seem to be getting through to the Django

For some reason, the data I am trying to post to /api/recipe/recipes/ is not showing up in my HTML {% extends 'base.html' %} {% block content %} <!DOCTYPE html> <html> <head> <script src="h ...

Minimizing the frequency of getElementById calls for the same HTML element

When dealing with repetitive function calls using the same element as a parameter, is it more effective to store the element as a global variable? For instance, imagine a function that is triggered on every key press and takes an element as an argument. ...

Using Plotly.js within a deleted Vue.js component may result in displaying an incorrect chart

Issue I am facing a problem with deleting one of the components that contains a chart. Even after deleting the component, the chart remains visible. To see an example, check out this jsfiddle: https://jsfiddle.net/m4ywp5fc/4/ Solution Attempted I attem ...