我有以下路线:
router.get('/blog/:title', function (req, res) {
blog.getBlogPost(req, function (data) {
res.render('blog', data);
});
});
和函数getBlogPost
exports.getBlogPost = function (req, callback) {
// TODO SAFE PARAM
// TODO ERR Handling
var title = req.param('title');
Blog.findOne({permaLink: title}).exec(function (err, data) {
var date = new Date(+data.timestamp);
data.tags = createHashtagsFromCSV(data.tags);
data.date = month_names_long[date.getMonth()] + " " + date.getDate() + ", " + date.getFullYear();
callback(data);
});
// not found
callback({});
};
奇怪的是我得到了以下节点控制台日志
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (http.js:689:11)
at ServerResponse.header (C:\Users\Michael\Desktop\qcode\node_modules\express\lib\response.js:662:10)
at ServerResponse.send (C:\Users\Michael\Desktop\qcode\node_modules\express\lib\response.js:146:12)
at fn (C:\Users\Michael\Desktop\qcode\node_modules\express\lib\response.js:896:10)
at View.exports.renderFile [as engine] (C:\Users\Michael\Desktop\qcode\node_modules\jade\lib\jade.js:325:12)
at View.render (C:\Users\Michael\Desktop\qcode\node_modules\express\lib\view.js:76:8)
at Function.app.render (C:\Users\Michael\Desktop\qcode\node_modules\express\lib\application.js:527:10)
at ServerResponse.res.render (C:\Users\Michael\Desktop\qcode\node_modules\express\lib\response.js:900:7)
at module.exports (C:\Users\Michael\Desktop\qcode\app.js:65:9)
at Layer.handle_error (C:\Users\Michael\Desktop\qcode\node_modules\express\lib\router\layer.js:58:5)
我已经在我的项目中做了几次这样的事情,但这次它抛出了这个错误,我现在想知道为什么?知道为什么吗?
答案 0 :(得分:1)
你的回叫电话两次。所以第二个抛出头错误
制作类似
的内容Blog.findOne({permaLink: title}).exec(function (err, data) {
if (!data) {
callback({});
return;
}
var date = new Date(+data.timestamp);
data.tags = createHashtagsFromCSV(data.tags);
data.date = month_names_long[date.getMonth()] + " " + date.getDate() + ", " + date.getFullYear();
callback(data);
});