I have some really strange problem with nodejs and express.
我对nodejs和express有一些奇怪的问题。
One of my functions that handles request, have to get something from DB and send it as JSON to client.
处理请求的函数之一,必须从DB获得一些东西,并将它作为JSON发送给客户端。
So it goes like this:
它是这样的:
- Get request
- Get请求
- Call DB
- 调用数据库
- Process data and pack it to JSON
- 处理数据并将其打包为JSON
- response.json(JSON)
- response.json(JSON)
Normally it will go all OK but if there is timeout between 2 and 3 because it is asynchronously it will automatically create response and there will be error "Can\'t set headers after they are sent." when I call 4
通常它会运行正常,但是如果在2和3之间有超时,因为它是异步的,它会自动创建响应,当我调用4时,会出现“Can 't set headers after they are sent”的错误
Does anyone else have this problem? Is there any normal way to handle it or I just have to check if response._header is allready set?
有人有这个问题吗?有没有什么正常的方法来处理它,或者我只需要检查一下是否有响应。_header是已经准备好了吗?
exports.appstimebygroup = function (req, res) {
var resp = {};
var clientId = Webapi.extractClientId(req);
AppTime.getByGroupId(clientId, req.body.groupId, function(error, appstime){
if (error) {
handleError(error);
resp.returnCode = 0;
resp.message = "Some error have happened, please contact support!";
res.setHeader("Content-Type", "application/json");
res.json(resp);
return;
}
resp.returnCode = 1;
resp.appstime = appstime;
if(res._header){
console.log("header allready set!");
return;
}
res.setHeader("Content-Type", "application/json");
res.json(resp);
});
};
And AppTime.getByGroupId has asynchronous call inside.
和AppTime。getByGroupId内部有异步调用。
1 个解决方案
#1
0
Ok, problem is multipart-form-data handling timeout.
问题是多部分数据处理超时。
When that happens it calls next(err).
当发生这种情况时,它调用next(err)。
form.on('error', function(err){
if (!options.defer) {
err.status = 400;
next(err);
}
done = true;
});
By default on error it will do res.send(400) and when normally gets to code that you wanted to be executed there is problem.
默认情况下,它将执行res.send(400),当通常到达要执行的代码时,会出现问题。
#1
0
Ok, problem is multipart-form-data handling timeout.
问题是多部分数据处理超时。
When that happens it calls next(err).
当发生这种情况时,它调用next(err)。
form.on('error', function(err){
if (!options.defer) {
err.status = 400;
next(err);
}
done = true;
});
By default on error it will do res.send(400) and when normally gets to code that you wanted to be executed there is problem.
默认情况下,它将执行res.send(400),当通常到达要执行的代码时,会出现问题。