【发布时间】:2014-03-30 08:47:40
【问题描述】:
最近我在一个新项目上工作,这个项目在 nodejs 中使用 JavaScript 回调。现在我们使用KOA,但是当我们尝试使用 ES6 生成器和回调时就会出现问题。
//Calback function
function load(callback){
result = null;
//Do something with xmla4js and ajax
callback(result);
return result;
}
现在在KOA 我需要调用load 并将json 响应给客户端,所以我使用下面的代码:
router= require('koa-router');
app = koa();
app.use(router(app));
app.get('load',loadjson);
function *loadJson(){
var that = this;
load(function(result){
that.body = result;
});
}
但我得到这个错误:
_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:40: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:2113:50)
at Object.ajaxOptions.complete (G:\NAP\node_modules\xmla4js\src\Xmla.js:2024:34)
【问题讨论】:
-
这个错误是因为多个 res.send()。响应已发送,您再次尝试发送。
-
在加载函数中我需要 ajax,因为 xmla4js 使用它。但我可以记录导致负载只能发送到客户端。这个问题是 ajax 还是 xmla4js 发生的?
标签: javascript node.js callback koa