Domanda

Sto usando il nodo-sapone da integrare con un API basato su sapone esterno.Con questa libreria, un oggetto client viene creato in runtime in base alla WSDL.Pertanto, l'oggetto Client SOAP non è valido al momento della progettazione.Questo è un problema cercando di utilizzare la biblioteca della promessa Q.La biblioteca Q è vincolante / valutando la chiamata del metodo troppo presto, prima che sia definita.Ecco lo snippet di codice da illustrare.

Questo è il codice client utilizzando una catena di promessa:

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

Questo è lo snippet del codice di servizio per il login () che funziona bene.

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();
            }
        });
    });
};
.

Questo è il problema.self.innotasclient.Findenty non esiste fino a dopo 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
    })
}
.

Questo è l'errore di runtime:

        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)
.

Questo codice funziona bene con i callback.Qualche idea su come far funzionare questo con la Biblioteca Promise?

È stato utile?

Soluzione

.

La biblioteca Q è vincolante / valutando la chiamata del metodo troppo presto, prima che sia definita

No - Sei:

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

Qui stai chiamando getProject() prima di passare i risultati nel metodo then.Tuttavia, then si aspetta una funzione callback , che verrà chiamata quando la promessa se risolta.Useresti

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

Se il client non esiste fino a quando il metodo createClient rendisce, il client dovrebbe essere il risultato di tale funzione - lascia che restituisca una promessa per il client!

Il tuo lib dovrebbe quindi assomigliare a questo:

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
    });
};
.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top