P. ninvoke com o nó soap biblioteca de avaliação chamada de função muito cedo
Pergunta
Eu estou usando o nó de sabão para integrar com um externo baseado em soap API.Com esta biblioteca, um objeto de cliente é criado em tempo de execução com base no WSDL.Portanto, o objeto de cliente soap não é válido em tempo de design.Este é um problema ao tentar usar o Q promessa de biblioteca.O Q biblioteca é obrigatório/avaliar a chamada do método cedo demais, antes de ser definido.Aqui está o trecho de código para ilustrar.
Este é o código de cliente usando uma promessa de cadeia:
innotas.login(user,pass)
.then(innotas.getProjects())
.then(function () {
console.log('finished');
});
Este é o serviço snippet de código para fazer o login (), que funciona bem.
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();
}
});
});
};
Esse é o problema.auto.innotasClient.findEntity não existe até que depois de 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
})
}
Este é o erro de tempo de execução:
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)
Este código funciona bem com as chamadas de retorno.Qualquer ideia de como chegar a este trabalho, com a promessa de biblioteca?
Solução
O Q biblioteca é obrigatório/avaliar a chamada do método cedo demais, antes de ser definido
Sem você são:
innotas.login(user,pass) .then(innotas.getProjects())
Aqui você está chamando getProject()
antes de passar os seus resultados para o then
o método.No entanto, then
espera um chamada de retorno a função, que será chamada quando a promessa se resolvido.Você usaria
innotas.login(user,pass).then(function(loginresult) {
innotas.getProjects()
})
Se o cliente não existe, até o createClient
método de rendimentos, o cliente deve ser o resultado da função - deixe-o retorno de uma promessa para o cliente!
Sua lib, em seguida, deve olhar como este:
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
});
};