【发布时间】:2014-01-25 15:01:50
【问题描述】:
我的 app.js 中有一个 get
app.get('/api/personnel', api.personnel);
调用此函数作为回调从 mongo 加载一些数据:
exports.personnel = function(req, res) {
var docs;
db.personnel.find(function(err, docs) {
if (err) {
logError(err);
} else {
res.json({
personnel: docs
});
}
});
};
这很好用,但我真的希望能够在函数完成时调用回调以进行测试:
exports.personnel = function(req, res, callback) {
var docs;
db.personnel.find(function(err, docs) {
if (err) {
logError(err);
} else {
res.json({
personnel: docs
});
}
callback();
});
从实时应用程序调用该函数时callback() 为空并给我一个错误:
Error: Can't set headers after they are sent.
如何让 get 调用我的回调?
【问题讨论】:
-
Express 中的第三个参数总是为
next()回调保留(在中间件中找到)