3

I am using the xdmp.eval function to search and return a document in my final database to use during my harmonization process.

let finalDoc = xdmp.eval(
"fn.head(cts.search(cts.jsonPropertyValueQuery('Id',id, 
['exact']),['unfiltered','score-zero']))",
     {'id':id},
     {"database" : xdmp.database("data-hub-FINAL")});

The document is returned as a sequence with this structure:

{"SourceSystemName":"",
  "BatchDtTm":"06/20/2018 15:05:15",
  "SubjectArea":"Customer",
  "DocumentType":"Registration",
   "Id":"100", 
     "Contact":[
       {"CustomerId":"1",
        "FirstName":"",
        "LastName":"",
        "EmailId":""
       },
       {"CustomerId":"2",
        "FirstName":"",
        "LastName":"",
        "EmailId":""
       }            
     ]
}

I need to iterate through each customer in the Contact array using finalDoc.Contact.forEach(). However, when I use fn.head(finalDoc.toArray()) or fn.head(finalDoc.toObject()) to change the finalDoc sequence to an array, my forEach function fails with the error "Cannot read property 'forEach' of undefined". When I simply just try to output the contact array by doing finalDoc.Contact (or finalDoc[0].Contact), I get a "Null" output.

How do I grab the Contact array out of the sequence and iterate through it using forEach? Thanks!

Hank
  • 131
  • 4

2 Answers2

3

You have the right pieces, just the wrong order. fn.head doesn't take an array, so it has no effect in fn.head(finalDoc.toArray()): you'll still have an array. You could convert the sequence to an array and then use the array index:

finalDoc.toArray()[0].Contact.forEach(...)

or you can use the fn.head on the originally eval'd Sequence

fn.head(finalDoc).Contact.forEach(...)
BenW
  • 408
  • 2
  • 9
-1

var myobj = {"SourceSystemName":"",
  "BatchDtTm":"06/20/2018 15:05:15",
  "SubjectArea":"Customer",
  "DocumentType":"Registration",
   "Id":"100", 
     "Contact":[
       {"CustomerId":"1",
        "FirstName":"",
        "LastName":"",
        "EmailId":""
       },
       {"CustomerId":"2",
        "FirstName":"",
        "LastName":"",
        "EmailId":""
       }            
     ]
}

myobj["Contact"].forEach(function(item){
console.log(item);
})

I think you don't need to call toArray() if it is an object already.

Lukasz
  • 1,807
  • 8
  • 12