0

I'm trying to return a search result. For this to work properly I understand I need a promise. When Googling this I get crazy different approaches. I'm looking for the most simple way.

This is my current script but it seems to be promised something but it does not get delivered, it keeps waiting.

    var http = require('http');
var marklogic = require("marklogic");
var Promise = require('promise');

// File containing MarkLogic sever connection settings
var conn = require("./nodeMLserverSettings.js").connection;

// Start connection with database
var db = marklogic.createDatabaseClient(conn);

// Build a query in variable q
var q = marklogic.queryBuilder;

// Test MarkLogic NodeJS API
// console.dir(marklogic);

function promisedText(query, q, db) {
  return new Promise(function (fulfill, reject){
    db.documents.query(q.where(q.parsedFrom(query))).result().done(function (res){
      try {
        fulfill(JSON.parse(res));
      } catch (ex) {
        reject(ex);
      }
    }, reject);
  });
}

promisedText('Consulting', q, db);

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
Thijs
  • 1,423
  • 15
  • 38
  • edited my question but it's still marked as duplicate – Thijs Jun 25 '15 at 15:54
  • 2
    The .result() returns a promise. You don't need to create one. Your code isn't actually doing anything. It's listening on port 1337, but only returns a static response. I'd advise starting with something like Express for the HTTP server stuff. Take a look at https://github.com/marklogic/Geophoto/blob/master/routes.js#L106-L111 to see how to use the MarkLogic Node.js Client API with Express. – Justin Makeig Jun 25 '15 at 17:37

0 Answers0