Roy Tang

Programmer, engineer, scientist, critic, gamer, dreamer, and kid-at-heart.

Blog Notes Photos Links Archives About

All entries tagged node.js.

You can subscribe to an RSS feed of this list.

Dec 2017

  • I have a query using MarkLogic node.js that basically boils down to something like this:

    db.documents.query(qb.where(qb.collection('test'))).stream()
    .on('data', function(row) {
        console.log("Stream on data");
    })
    .on('end', function() {
        console.log("Stream on end");
    })
    .on('error', function(error) {
        console.log(error);
    })
    ;
    

    Now, for a certain collection we have in our database, the ‘end’ function doesn’t fire, i.e. I never see “Stream on end” appear in the log. There’s no error or anything, processing just stops. It’s only for this particular collection, other collections seem fine.

    If I query documents in that collection directly using other methods such as qb.value() without using qb.collection(), the end event fires correctly. But once I add qb.collection() into the mix (using qb.and), the end event doesn’t fire.

    I’m unsure how to debug this, as this is my first time trying to use streams in the nodejs client library. Any advice as to what I can check?

    Thanks!

Oct 2017

  • I’m trying out the Roxy deployer. The Roxy app was created using the default app-type. I setup a new ML 9 database, and I ran “ml local bootstrap” using the default ports (8040 and 8041)

    Then I setup a node application. I tried the following (sample code from https://docs.marklogic.com/jsdoc/index.html)

    var marklogic = require('marklogic');
    var conn = {
        host: '192.168.33.10',  
        port: 8040,
        user: 'admin',
        password: 'admin',
        authType: 'DIGEST'
    }
    
    var db = marklogic.createDatabaseClient(conn);
    
    db.createCollection(
      '/books',
      {author: 'Beryl Markham'},
      {author: 'WG Sebald'}
      )
    .result(function(response) {
        console.log(JSON.stringify(response, null, 2));
      }, function (error) {
        console.log(JSON.stringify(error, null, 2));
      });
    

    Running the script gave me an error like:

    $ node test.js
    {
      "message": "write document list: cannot process response with 500 status",
      "statusCode": 500,
      "body": "<error:error xsi:schemaLocation=\"http://marklogic.com/xdmp/error error.xsd\" xmlns:error=\"http://marklogic.com/xdmp/error\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n  <error:code>XDMP-IMPMODNS</error:code>\n  <error:name>err:XQST0059</error:name>\n  <error:xquery-version>1.0-ml</error:xquery-version>\n  <error:message>Import module namespace mismatch</error:message>\n  <error:format-string>XDMP-IMPMODNS: (err:XQST0059) Import module namespace http://marklogic.com/rest-api/endpoints/config does not match target namespace http://marklogic.com/rest-api/endpoints/config_DELETE_IF_UNUSED of imported module /MarkLogic/rest-api/endpoints/config.xqy</error:format-string>\n  <error:retryable>false</error:retryable>\n  <error:expr/>\n  <error:data>\n    <error:datum>http://marklogic.com/rest-api/endpoints/config</error:datum>\n    <error:datum>http://marklogic.com/rest-api/endpoints/config_DELETE_IF_UNUSED</error:datum>\n    <error:datum>/MarkLogic/rest-api/endpoints/config.xqy</error:datum>\n  </error:data>\n  <error:stack>\n    <error:frame>\n      <error:uri>/roxy/lib/rewriter-lib.xqy</error:uri>\n      <error:line>5</error:line>\n      <error:column>0</error:column>\n      <error:xquery-version>1.0-ml</error:xquery-version>\n    </error:frame>\n  </error:stack>\n</error:error>\n"
    }
    

    If I change the port to 8000 (the default appserver that inserts into Documents), the node function executes correctly as expected. I’m not sure if I need to configure anything else with the Roxy-created appserver so that it works with the node.js application.

    I’m not sure where the “DELETE_IF_UNUSED” part in the error message is coming from either. There doesn’t seem to be any such text in the configuration files generated by Roxy.

    Edit: When accessing 192.168.33.10:8040 via the browser, I get a an xml with a similar error:

    <error:error xsi:schemaLocation="http://marklogic.com/xdmp/error error.xsd" xmlns:error="http://marklogic.com/xdmp/error" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <error:code>XDMP-IMPMODNS</error:code>
      <error:name>err:XQST0059</error:name>
      <error:xquery-version>1.0-ml</error:xquery-version>
      <error:message>Import module namespace mismatch</error:message>
      <error:format-string>XDMP-IMPMODNS: (err:XQST0059) Import module namespace http://marklogic.com/rest-api/endpoints/config does not match target namespace http://marklogic.com/rest-api/endpoints/config_DELETE_IF_UNUSED of imported module /MarkLogic/rest-api/endpoints/config.xqy</error:format-string>
      <error:retryable>false</error:retryable>
      <error:expr/>
      <error:data>
        <error:datum>http://marklogic.com/rest-api/endpoints/config</error:datum>
        <error:datum>http://marklogic.com/rest-api/endpoints/config_DELETE_IF_UNUSED</error:datum>
        <error:datum>/MarkLogic/rest-api/endpoints/config.xqy</error:datum>
      </error:data>
      <error:stack>
        <error:frame>
          <error:uri>/roxy/lib/rewriter-lib.xqy</error:uri>
          <error:line>5</error:line>
          <error:column>0</error:column>
          <error:xquery-version>1.0-ml</error:xquery-version>
        </error:frame>
      </error:stack>
    </error:error>
    

    If it matters, MarkLogic version is 9.0-3.1. It’s a fresh install too.

    Any advice?

Jun 2017

Dec 2016

  • I tried to insert the following test document:

    db.documents.write(
        {
            uri: "/test/doc1.json",
            contentType: "application/json",
            collections: "test",
            content: {
                name : "Peter",
                hobby: "Sleeping",
                other: "Some other info",
                "triple": {
                    "subject": {   
                        "datatype": "http://example.com/name/",  
                        "value": "Peter"   
                    },   
                    "predicate": {     
                        "datatype": "http://example.com/relation/",  
                        "value": "livesin"   
                    },   
                    "object": {     
                        "datatype": "http://example.com/location/",  
                        "value": "Paris"   
                    }
              	}
            }
        }
      ).
      result(function(response){
        console.log("Done loading");
      }); 
    

    Then I queried as follows:

    var query = [
      'SELECT ?s ?p ?o' ,
      'WHERE { ?s ?p ?o }' ,
    ];
    db.graphs.sparql('application/sparql-results+json', query.join('\n')
    ).result(function (result) {
      console.log(JSON.stringify(result, null, 2));
    }, function(error) {
      console.log(JSON.stringify(error, null, 2));
    });
    

    The results showed me the values of the triple, but what if I also want to get the entire document where the triple was embedded? Is it also possible to filter by other fields in the document?

Nov 2016

  • I want to retrieve all documents in my MarkLogic db that have month=November and also group them by name, and get the count of records per name. I know I can get the frequency per name by using valuesBuilder with a range index on the name field, but how can I filter this result so that I only get the count of records for the month of November?

    Supposedly valuesBuilder.fromIndexes().where() can do the filtering, but I don’t know what to pass here and examples online seem to be sparse.