Search code examples
javascripttransactionsmarklogicinvoke

JavaScript invoke transactions in MarkLogic


I have xml document:

<product-inventory>
  <product>
    <name>shoe</name>
    <price>100</price>
    <date>2020-05-31</date>
  </product>
  <product>
   <name>dress</name>
    <price>100</price>
    <date>2020-06-01</date>
  </product>
  <product>
   <name>dress</name>
    <price>200</price>
    <date>2020-06-02</date>
  </product>
</product-inventory>

I wrote a program to update the 2nd price element, audit such update, finally retrieve the updated document. The program contains three functions. I still see the old document when the program reaches the final retrieval step. If I open another window, I do see the updated document.

<product-inventory>
  <product>
    <name>shoe</name>
    <price>100</price>
    <date>2020-05-31</date>
  </product>
  <product>
   <name>dress</name>
    <price>700</price>
    <date>2020-06-01</date>
    <audit>
      <modifiedBy>admin</modifiedBy>
      <modifiedDate>2020-06-02T16:12:14.9664853-04:00</modifiedDate>
    </audit>
  </product>
  <product>
   <name>dress</name>
    <price>200</price>
    <date>2020-06-02</date>
  </product>
</product-inventory>

The gist of the functions:

function  updateDoc() {
**************
   xdmp.nodeReplace(
**************
};

function auditHistory() {
*********************
   xdmp.nodeInsertAfter(
*********************
};

function retrieveDoc() {
  return fn.collection('product')
};

xdmp.invokeFunction(function(){
  declareUpdate();
  updateDoc();
  auditHistory();
}),
retrieveDoc();

Can someone explain why I still see the old document when the program reaches retrieveDoc()?


Solution

  • I believe you need to wrap retrieveDoc() into another xdmp.invokeFunction to see the updated document.

    xdmp.invokeFunction(() => {
      return retrieveDoc()
    })