1

I am trying to use when.js with ddpclient. The code I have written is at the bottom of the post. When I attempt to use this, I get the following error below. Any ideas on how to get around this error? I am aware of another DDPclient that uses promises, but I am not keen to add an additional promise library.

Potentially unhandled rejection TypeError: Object #<Object> has no method '_nextId'
    at DDPClient.call (/Source/proj/node_modules/ddp/lib/ddp-client.js:329:17)
    at /Source/tellme/updater/node_modules/when/node.js:89:7
    at tryCatchReject (/Source/proj/node_modules/when/lib/makePromise.js:790:14)
    at FulfilledHandler.when (/Source/tellme/updater/node_modules/when/lib/makePromise.js:621:9)
    at ContinuationTask.run (/Source/tellme/updater/node_modules/when/lib/makePromise.js:741:24)
    at Scheduler._drain (/Source/proj/node_modules/when/lib/scheduler.js:56:14)
    at Scheduler.drain (/Source/proj/node_modules/when/lib/scheduler.js:21:9)
    at process._tickCallback (node.js:419:13)
    at Function.Module.runMain (module.js:499:11)
    at startup (node.js:119:16)

Code below:

"use strict";
var when           = require('when'),
    node           = require('when/node'),
    DDPClient      = require('ddp');


var ddpclient = new DDPClient({
    host: "localhost",
    port: 3000
});

var ddpconnect = node.lift(ddpclient.connect);
var ddpcall = node.lift(ddpclient.call);

//var ddpConnectPromise = node.lift(ddpclient.connect);

var obj = {"name": "john","age":25};

when(ddpconnect).then
(ddpcall("processObj", obj)).
    catch(function (error) {
        console.log(error);
    }).
    done();

EDIT: The following appears to get me closer, but I encounter a [TypeError: Object processObj has no method 'addListener'] error.

"use strict";
var when           = require('when'),
    node           = require('when/node'),
    DDPClient      = require('ddp');

var ddpConnectPromise = node.liftAll(DDPClient);

var ddpclient = new ddpConnectPromise({
    host: "localhost",
    port: 3000
});

var obj = {"name": "john","age":25};
when(ddpclient.connect).then(function (ddpclient) {
    ddpclient.call("processObj", sampleJSON);
}).
    catch(function (error) {
        console.log(error);
    }).
    done();
user1074891
  • 279
  • 3
  • 14
  • [It seems that using `liftAll` on objects with prototypes doesn't work that well.](https://github.com/cujojs/when/issues/294) – user3374348 Jun 19 '14 at 08:45

1 Answers1

0

If you don't mind using bluebird, it has a promisifyAll you could use:

var Promise = require("bluebird");
Promise.promisifyAll(require("ddp").prototype);
var DDPClient = require("ddp");    

var ddpclient = new DDPClient({
  host: "localhost",
  port: 3000,
  /* optional: */
  auto_reconnect: true,
  auto_reconnect_timer: 500,
  use_ejson: true,           // Use Meteor's EJSON to preserve certain data types.
  use_ssl: false,
  maintain_collections: true // Set to false to maintain your own collections.
});


var obj = {"name": "john","age":25};

ddpclient.connectAsync().then(function(ddpclient) {
    return ddpclient.callAsync("process", {}); // params is a required argument
}).then(function(callResult) {

}); // Logging errors is useless when using bluebird, so leave the .catch out
Esailija
  • 138,174
  • 23
  • 272
  • 326
  • I'd prefer to stick with When, but tried out the above regardless. I encountered the following error however: TypeError: Object [object Object] has no method 'connectAsync'. Does promisifyAll also not work well with prototypes perhaps? – user1074891 Jun 20 '14 at 00:28
  • I have the Async methods now after a few mods. connectAsync doesn't appear to return anything that I can pass into the next function however. – user1074891 Jun 20 '14 at 10:36
  • @user1074891 promisifyAll works as shown in the answer, otherwise you could use When.js – Esailija Jun 22 '14 at 10:23
  • @user1074891 seems ddp doesn't export a module but a class directly, I have modified the answer – Esailija Jun 22 '14 at 10:29
  • Thanks for getting back to me Esailija, but when I use the above code, I receive the error posted in my first comment. – user1074891 Jun 22 '14 at 10:34
  • @user1074891 I have modified the code after that, take a look at the edit. I don't get any error :) – Esailija Jun 22 '14 at 20:45
  • Thanks @Esailija. Some progress now as it appears I can use the connectAsync method. :) I am now encountering the following error however: "Possibly unhandled TypeError: Cannot call method 'callAsync' of undefined" – user1074891 Jun 23 '14 at 03:21