Impossibile impostare l'intestazione in Koa quando si utilizza la callback
Domanda
Recentemente ho lavorato su un nuovo progetto che ha utilizzato callback JavaScript.E stavo usando koa framework.Ma quando ho chiamato questa rotta:
function * getCubes(next) {
var that = this;
_OLAPSchemaProvider.LoadCubesJSon(function(result) {
that.body = JSON.stringify(result.toString());
});
}
.
Ottengo questo errore:
_http_outgoing.js:331
throw new Error('Can\'t set headers after they are sent.');
^
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:331:11)
at Object.module.exports.set (G:\NAP\node_modules\koa\lib\response.js:396:16)
at Object.length (G:\NAP\node_modules\koa\lib\response.js:178:10)
at Object.body (G:\NAP\node_modules\koa\lib\response.js:149:19)
at Object.body (G:\NAP\node_modules\koa\node_modules\delegates\index.js:91:31)
at G:\NAP\Server\OlapServer\index.js:42:19
at G:\NAP\Server\OlapServer\OLAPSchemaProvider.js:1599:9
at _LoadCubes.xmlaRequest.success (G:\NAP\Server\OlapServer\OLAPSchemaProvider.js:1107:13)
at Object.Xmla._requestSuccess (G:\NAP\node_modules\xmla4js\src\Xmla.js:2110:50)
at Object.ajaxOptions.complete (G:\NAP\node_modules\xmla4js\src\Xmla.js:2021:34)
. Soluzione
Il problema è che la tua chiamata ASYNC LoadCubesJSon()
richiede un po 'di tempo per tornare, ma KOA non ne è a conoscenza e continua con il flusso di controllo.Questo è fondamentalmente ciò che il yield
è per.
Gli oggetti "spostabili" includono promesse, generatori e thunks (tra gli altri).
I Personalmente preferisco creare manualmente una promessa con il Libreria 'Q' .Ma puoi usare qualsiasi altra biblioteca promessa o node-thunkify
per creare un thunk.
Qui è un esempio corto ma funzionante con Q
:
var koa = require('koa');
var q = require('q');
var app = koa();
app.use(function *() {
// We manually create a promise first.
var deferred = q.defer();
// setTimeout simulates an async call.
// Inside the traditional callback we would then resolve the promise with the callback return value.
setTimeout(function () {
deferred.resolve('Hello World');
}, 1000);
// Meanwhile, we return the promise to yield for.
this.body = yield deferred.promise;
});
app.listen(3000);
.
Quindi il tuo codice sarebbe il seguente:
function * getCubes(next) {
var deferred = q.defer();
_OLAPSchemaProvider.LoadCubesJSon(function (result) {
var output = JSON.stringify(result.toString());
deferred.resolve(output);
});
this.body = yield deferred.promise;
}
.