【发布时间】:2017-09-23 12:16:53
【问题描述】:
我正在尝试在我的代码中查找错误,但其他主题中没有任何内容适合。
有 app.js 代码,其中包含来自 Express 模块的get 方法:
app.get('/notes', notesController.all);
有notesController.js代码,导出到app.jscreate方法:
exports.all = function (req, res) {
Notes.all(function(err, docs){
if(err){
console.log(err);
return res.sendStatus(500);
}
res.send(docs);
})
};
在model这个代码:
exports.all = function (cb) {
db.get().collection('notes').find().toArray(function (err, docs) {
cb(err,docs);
})
};
应用程序因此错误而崩溃:
process.nextTick(function() { throw err; }); ^错误:发送后无法设置标头。 在 ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:357:11) 在 ServerResponse.header (O:\OGPI6\node_modules\express\lib\response.js:725:10) 在 ServerResponse.json (O:\OGPI6\node_modules\express\lib\response.js:253:10) 在 ServerResponse.send (O:\OGPI6\node_modules\express\lib\response.js:158:21) 在 O:\OGPI6\controllers\notes.js:9:13 在 O:\OGPI6\models\notes.js:6:9 在 handleCallback (O:\OGPI6\node_modules\mongodb\lib\utils.js:120:56) 在 O:\OGPI6\node_modules\mongodb\lib\cursor.js:860:16 在 handleCallback (O:\OGPI6\node_modules\mongodb-core\lib\cursor.js:171:5) 在 setCursorDeadAndNotified (O:\OGPI6\node_modules\mongodb-core\lib\cursor.js:505:3)
在我看来,回调函数中只有“控制器”的错误:
if(err){
console.log(err);
return res.sendStatus(500);
}
res.send(docs);
但我认为当发生错误时它必须终止函数并返回sendStatus(500),但在控制台中记录错误后它会尝试返回res.send(docs),然后应用程序崩溃,因为它正在发送第二个标头。它看起来不错,但不起作用。谁能指出我失败的方式?
【问题讨论】:
-
您是否认为您的
exports.all方法可能会多次触发回调。您是否尝试在回调的开头添加console.log? -
我同意@BenjiLees,看起来回调被调用了两次。
标签: javascript node.js mongodb express