我正在使用node-soap 与外部基于soap 的API 集成。使用该库,可以在运行时基于 WSDL 创建客户端对象。因此,soap 客户端对象在设计时无效。这是尝试使用 Q Promise 库时遇到的问题。Q 库在定义方法之前过早地绑定/评估方法调用。这是用于说明的代码片段。

这是使用承诺链的客户端代码:

innotas.login(user,pass)
.then(innotas.getProjects())
.then(function () {
    console.log('finished');
});

这是 login() 的服务代码片段,工作正常。

this.login = function login (user, password) {
    var deferred = q.defer();

    // some stuff here

    return self.createClient()
    .then(function () {
        self.innotasClient.login({ username: self.innotasUser, password: self.innotasPassword }, function(err, res) {
            if (err) {
              console.log('__Error authenticating to service: ', err);
              deferred.reject(err);
            } else {
              self.innotasSessionId = res.return;
              console.log('Authenticated: ', self.innotasSessionId);
              deferred.resolve();
            }
        });
    });
};

这就是问题。self.innotasClient.findEntity 在 CreateClient() 之后才存在

this.getProjects = function getProjects (request) {

    // initiation and configuration stuff here

    // CALL WEB SERVICE
    self.innotasClient.findEntity(req, function findEntityCallback (err, response) {
      if (err) {
        deferred.reject(err);
      } else {
        deferred.resolve(response);
      }
    })

    return deferred.promise;

    // alternate form using ninvoke
    return q.ninvoke(self.innotasClient, 'findEntity', req).then(function (response) {
        // stuff goes here
    }, function (err) {
        // err stuff goes here
    })
}

这是运行时错误:

        self.innotasClient.findEntity(req, function findEntityCallback (err, r
                           ^
TypeError: Cannot call method 'findEntity' of null
    at getProjects (/Users/brad/Workspaces/BFC/InnotasAPI/innotas.js:147:28)
    at Object.<anonymous> (/Users/brad/Workspaces/BFC/InnotasAPI/app.js:13:15)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)

这段代码与回调一起工作得很好。知道如何让它与承诺库一起使用吗?

有帮助吗?

解决方案

Q 库在定义方法之前过早地绑定/评估方法调用

不 - 你是:

innotas.login(user,pass)
.then(innotas.getProjects())

在这里你正在呼唤 getProject() 在将其结果传递到之前 then 方法。然而, then 确实期望 打回来 函数,当承诺得到解决时将调用该函数。你会用

innotas.login(user,pass).then(function(loginresult) {
    innotas.getProjects()
})

如果客户端不存在直到 createClient 方法产生,客户端应该是 结果 该函数的一部分 - 让它为客户端返回一个承诺!

您的库应该如下所示:

this.login = function login (user, password) {
    // some stuff here
    return self.createClient().then(function(client) {
        return Q.ninvoke(client, "login", {
            username: self.innotasUser,
            password: self.innotasPassword
        }).then(function(res) {
            self.innotasSessionId = res.return;
            console.log('Authenticated: ', self.innotasSessionId);
            return client;
        }, function(err) {
            console.log('__Error authenticating to service: ', err);
            throw err;
        });
    });
};
this.getProjects = function getProjects (client) {
    // initiation and configuration stuff here

    // CALL WEB SERVICE
    return q.ninvoke(client, 'findEntity', req).then(function (response) {
        // stuff goes here
    }, function (err) {
        // err stuff goes here
    });
};
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top