我在获取记录时遇到以下错误

时间:2015-02-05 06:39:15

标签: node.js express

我收到此错误我在提交后收到此错误,并且我将获取记录它会出现此错误。当错误发生时我给出了消息它不是错误然后执行其他功能。我得到一些设置标题错误,所以请让我知道我在哪里错误

/home/hacker201501/VAIBHAO/ndd/node_modules/mongoose/lib/utils.js:419
        throw err;
              ^
Error: Can't set headers after they are sent.
    at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:331:11)
    at ServerResponse.res.set.res.header (/home/hacker201501/VAIBHAO/ndd/node_modules/express/lib/response.js:524:10)
    at ServerResponse.res.json (/home/hacker201501/VAIBHAO/ndd/node_modules/express/lib/response.js:189:36)
    at Promise.<anonymous> (/home/hacker201501/VAIBHAO/ndd/app/controllers/categories_controller.js:110:26)
    at Promise.<anonymous> (/home/hacker201501/VAIBHAO/ndd/node_modules/mongoose/node_modules/mpromise/lib/promise.js:157:8)
    at Promise.EventEmitter.emit (events.js:107:17)
    at Promise.emit (/home/hacker201501/VAIBHAO/ndd/node_modules/mongoose/node_modules/mpromise/lib/promise.js:79:38)
    at Promise.reject (/home/hacker201501/VAIBHAO/ndd/node_modules/mongoose/node_modules/mpromise/lib/promise.js:106:15)
    at Promise.error (/home/hacker201501/VAIBHAO/ndd/node_modules/mongoose/lib/promise.js:89:15)
    at Query.execFind (/home/hacker201501/VAIBHAO/ndd/node_modules/mongoose/lib/query.js:1664:13)

这里是我的getfunction

exports.get_subcategory = function(req, res){
    var subcategoryData = req.query;
    console.log('auth_token');
    console.log(req.query.auth_token);
    console.log('query');
    console.log(subcategoryData);
    User.findOne({auth_token: req.query.auth_token}, function(user_error, user){
        if (user_error) res.json({status: 'Error', message: 'Login require'});
        if (!user) res.json({status: 'Error', message: 'Invalid user'});
        if (user){
            SubCategory.find({category: req.query.category}, function(categoryError, subcatgory){
                if (categoryError) res.json({status: 'Error', message: 'Category error'});
                if (!subcatgory) res.json({status: 'Error', message: 'No category found'});
                if (subcatgory) {
                    res.json({status: 'OK', data: subcatgory});
                }
            })
        }
    })
}

我不明白发生了什么

1 个答案:

答案 0 :(得分:1)

我查看了您的代码,做了一些更改。 现在它应该工作,试试这个。

exports.get_subcategory = function(req, res){
    var subcategoryData = req.query;
    User.findOne({
                    auth_token: req.query.auth_token
                }, function(user_error, user){
                        if (user_error) 
                            res.json({status: 'Error', message: 'Login require'});
                        SubCategory.find(
                                        {category: req.query.category}, 
                                        function(categoryError, subcatgory){
                                            if (categoryError) 
                                                res.json({status: 'Error', message: 'Category error'});

                                            res.json({status: 'OK', data: subcatgory});

                                        }
                        )

                }
    )
}