好吧,我有以下代碼res.end一系列嵌套的回調內部
router.route('/getParkData/:parkLocationId').get(function(req, res){
var id = req.params.parkLocationId;
var parkdata = {};
parkInfo(id).then(function(data){
if(data.parking_type == 'FREE')
{
var json = JSON.stringify({
"error" : null,
"result" : null,
"message" : "free",
"error_code" : null
});
res.end(json);
}
else
return getTwowheelers(id)
}).then(function(data){
if(data[0].parking_type)
{
parkdata.parkingType = data[0].parking_type;
}
else
{
parkdata.parkingType = "paid";
}
if(data.length > 0)
{
parkdata.tw = data[0].rate.tw[0];
}
else
{
parkdata.tw= 0;
}
return getFourwheelers(id);
}).then(function(data){
console.log(data.length);
if(data.length > 0)
{
parkdata.fw = data[0].rate.fw[0];
}
else
{
parkdata.fw = 0;
}
res.json(mResponse.response(null, parkdata, "Success",200));
}).catch(function(err){
res.json(mResponse.response(null, null, "No data!",500));
});
});
所以我在做通過檢查條件和結束respons.Its做工精細速戰速決,但我得到這個錯誤。
Unhandled rejection Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:344:11)
at ServerResponse.header (C:\zoyoapp\node_modules\express\lib\response.js:719:10)
所以res.end沒有結束execution.Any方式,我可以讓它不顯示這個錯誤
請妥善縮進代碼。 – str
你的代碼似乎沒有適當的匹配花括號不完整的,你可以發佈完整的代碼 – Sridhar
@Sridhar嘿,我也做了編輯您想要res.end後'跳過承諾鏈執行其餘 –