marklogicmarklogic-9marklogic-dhfmarklogic-optic-api

Marklogic Optic Query in Server-Side JavaScript(.sjs) getting job failed


I am getting job failed while running Optic Query in .sjs file. It seems syntax issue in .sjs file for optic query. Can you please help me to provide the correct syntax to run optic query in .sjs file? Please find below code snippet.

const op = require('/MarkLogic/optic');
let k = op.fromView('example','sampleData').select(['firstName']).result();
let t = xs.string(k);
let employeeFirstName = fn.replace(EmployeeFirstName, 'John', t);

-here 'example' is schema name, sampleData is view name and 'firstName' is column name. Its working fine in marklogic qconsole with java script query type but getting issue in .sjs file.


Solution

  • I wouldn't expect the following statement to execute because the expression on the right-hand side of the assignment is trying to evaluate a variable (employeeFirstName) that is undefined until the assignment occurs:

    let employeeFirstName = fn.replace(EmployeeFirstName, 'John', t);
    

    Can you explain more about what you're trying to do or (if that's not representative of the actual code) provide the actual code?

    One other point -- result() returns a Sequence. By default, that's a Sequence of objects, each with a property name corresponding to the column name.

    For that reason, the following assignment might do a better job of getting the value from the result:

    let t = fn.head(k)['example.sampleData.firstName'];
    

    In QueryConsole, it's often helpful to return the result() from an Optic query so you can see the result data structure.

    Finally, a main module would typically end in a value rather than an assignment.

    Hoping that helps,