2017-10-04 27 views
0

一次經過兩次請求發送這個錯誤顯示,我的服務器關閉節點JS用MongoDB的錯誤:無法設置頭他們被送到

我使用此代碼:

exports.getAllCompany = function (req, res) {   
Company.find({}, function (err, record) { 
    if (err) { 
     res.json({ 
      type: false, 
      resultData: "Error occured: " + err 
     }); 
    } else { 
     res.json({ 
      type: true, 
      company: record 
     }); 
    } 
});} 

錯誤信息:

Error: Can't set headers after they are sent. 
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:356:11) 
at ServerResponse.header (/app/node_modules/express/lib/response.js:730:10) 
at ServerResponse.send (/app/node_modules/express/lib/response.js:170:12) 
at ServerResponse.json (/app/node_modules/express/lib/response.js:256:15) 
at /app/server/mobile_api/AdminController/dashboard.js:279:17 
at Query.<anonymous> (/app/node_modules/mongoose/lib/model.js:3388:16) 
at /app/node_modules/kareem/index.js:259:21 
at /app/node_modules/kareem/index.js:127:16 
at _combinedTickCallback (internal/process/next_tick.js:73:7) 
at process._tickCallback (internal/process/next_tick.js:104:9) 
+0

什麼方法調用getAllCompany?問題應該在那裏...我想你也在那裏做res.send。 –

+0

getAllComapny調用post方法。 –

+0

你能顯示代碼嗎?像app.get(url,function(req,res){calls getAllCompany}) –

回答

2

最後經過很長時間才解決問題。按照我的回答誰有同樣的問題,

exports.getAllCompany = function (req, res) { 
    res.writeHead(200, {"Content-Type": "application/json"});   
    Company.find({}, function (err, record) { 
    if (err) { 
     res.end(JSON.stringify({ 
     type: false, 
     resultData: "Error occured: " + err 
     })); 
    } else { 
     res.end(JSON.stringify({ 
     type: true, 
     company: record 
    })); 
    } 
    }); 
} 
+0

內的一些不良配置鉤子請解釋您的代碼如何解決問題。 – jorisw

+0

只需添加一行** res.writeHead(200,{「Content-Type」:「application/json」})** & 將** res.json **更改爲** res.end ** –

+0

我的意思是,除非你解釋_how_代碼回答問題,否則在StackOverflow上粘貼代碼是沒有用的。 – jorisw

0

最可能的是,你在呼喚res.send()不止一次

共享/應用/服務器/暴徒ile_api/AdminController/dashboard.js文件會有幫助

+0

我添加了dashboard.js代碼。我編輯了我的問題。 –

+0

dashboard.js看起來不錯。 可能是在kareem(?) –

相關問題