Javascript 如何在承诺中使用DDPClient?

Javascript 如何在承诺中使用DDPClient?,javascript,node.js,meteor,promise,when-js,Javascript,Node.js,Meteor,Promise,When Js,我正试着用它。我写的代码在文章的底部。当我尝试使用它时,我得到以下错误。有没有关于如何避免这个错误的想法?我知道还有一个使用promises的库,但我不想再添加一个promise库 Potentially unhandled rejection TypeError: Object #<Object> has no method '_nextId' at DDPClient.call (/Source/proj/node_modules/ddp/lib/ddp-client.j

我正试着用它。我写的代码在文章的底部。当我尝试使用它时,我得到以下错误。有没有关于如何避免这个错误的想法?我知道还有一个使用promises的库,但我不想再添加一个promise库

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)
编辑:以下内容似乎更接近我,但我遇到了[TypeError:Object processObj没有方法'addListener']错误

如果您不介意使用,它有一个承诺,您可以使用:

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

我宁愿坚持使用When,但不管怎样,我还是尝试了上面的方法。但是,我遇到了以下错误:TypeError:Object[Object Object]没有方法“connectAsync”。promisifyAll是否也不能很好地与原型一起工作?在几个mod之后,我现在有了异步方法。connectAsync似乎没有返回我可以传递到下一个函数的任何内容。@user1074891 promisifyAll的工作原理如答案所示,否则您可以在何时使用。js@user1074891看起来ddp不是直接导出模块而是直接导出类,我修改了答案谢谢你回复我Esailija,但是当我使用上面的代码时,我收到第一条评论中的错误。
"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();
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