I am using node-soap to integrate with an external soap-based API. With this library, a client object is created at runtime based on the WSDL. Therefore, the soap client object is not valid at design time. This is a problem trying to use the Q promise library. The Q library is binding/evaluating the method call too early, before it is defined. Here is the code snippet to illustrate.
This is the client code using a promise chain:
innotas.login(user,pass)
.then(innotas.getProjects())
.then(function () {
console.log('finished');
});
This is the service code snippet for login() which works fine.
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();
}
});
});
};
This is the problem. self.innotasClient.findEntity does not exist until after 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
})
}
This is the runtime error:
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)
This code works fine with callbacks. Any idea how to get this to work with the promise library?