在节点中获取 header 重新发送错误

Getting header resend error in node

我有以下路由:

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({});
};

奇怪的是我得到了 Node 控制台日志

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)

我已经在我的项目中做过几次这样的事情,但这次它抛出了这个错误,我现在知道为什么了吗?知道为什么吗?

您的回调调用了两次。所以第二个抛出 header 错误

制作类似

的东西
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);
});