lixiang
lixiang

Reputation: 2001

marklogic save xml document with node-client-api?

I can't seem to save xml document into the marklogic with it's node-client-api. I had used the following code to try to save a dummy xml document

var marklogic = require('marklogic');
var my = require('../db/env.js');
var db = marklogic.createDatabaseClient(my.connInfo);

console.log("Write a single dummy xml document");

db.documents.write(
{
	uri: '/collegiate/testxml.xml',
	contentType: 'application/xml',
	content: '<entry-list><entry id="horror"></entry></entry-list>'
})

Then I used the following code to retrieve it:

var marklogic = require('marklogic');
var my = require('../db/env.js');
var db = marklogic.createDatabaseClient(my.connInfo);

console.log("Read a single xml document");

db.documents.read('/collegiate/testxml.xml')
.result().then(function(document) {
    //console.log('\nURI: ' + document.uri);
    for (var key in document) {
    	console.log("key: " + key + " value: " + document.key);
    }
}).catch(function(error) {
    console.log(error);
});

What I get from the output is:

Read a single xml document
key: 0 value: undefined

So how to save a xml document correctly?

Upvotes: 1

Views: 114

Answers (1)

ehennum
ehennum

Reputation: 7335

The issue is in the read code.

Because a client can read multiple documents, the read() request returns an array of documents.

So, try something like:

function(documents) {
  for (var i=0; i < documents.length; i++) {
    console.log(documents[i].content);
  }
}

The repository has some examples, though the focus is on JSON documents:

https://github.com/marklogic/node-client-api/blob/master/examples/read-documents.js#L27

Hoping that helps

Upvotes: 2

Related Questions