简体   繁体   English

发送后无法设置标题

[英]Can`t set headers after they are sent

I`m trying to find an error in my code, and nothing from other topics fits. 我正在尝试在我的代码中发现错误,而其他任何主题都不适合。

There is app.js code, which contains get method from Express module: 有app.js代码,其中包含Express模块​​中的get方法:

app.get('/notes', notesController.all);

There is notesController.js code, which exports to app.js create method: 有notesController.js代码,可将其导出到app.js create方法:

exports.all = function (req, res) {
    Notes.all(function(err, docs){
        if(err){
            console.log(err);
            return res.sendStatus(500);
        }
        res.send(docs);
    })
};

In model this code: model此代码:

exports.all = function (cb) {
    db.get().collection('notes').find().toArray(function (err, docs) {
        cb(err,docs);
    })
};

App crashes with this error: 应用程序因以下错误而崩溃:

 process.nextTick(function() { throw err; }); ^ 

Error: Can't set headers after they are sent. 错误:发送标头后无法设置标头。 at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:357:11) at ServerResponse.header (O:\\OGPI6\\node_modules\\express\\lib\\response.js:725:10) at ServerResponse.json (O:\\OGPI6\\node_modules\\express\\lib\\response.js:253:10) at ServerResponse.send (O:\\OGPI6\\node_modules\\express\\lib\\response.js:158:21) at O:\\OGPI6\\controllers\\notes.js:9:13 at O:\\OGPI6\\models\\notes.js:6:9 at handleCallback (O:\\OGPI6\\node_modules\\mongodb\\lib\\utils.js:120:56) at O:\\OGPI6\\node_modules\\mongodb\\lib\\cursor.js:860:16 at handleCallback (O:\\OGPI6\\node_modules\\mongodb-core\\lib\\cursor.js:171:5) at setCursorDeadAndNotified (O:\\OGPI6\\node_modules\\mongodb-core\\lib\\cursor.js:505:3) 在ServerResponse.json(O:\\ OGPI6 \\ node_modules)在ServerResponse.header(O:\\ OGPI6 \\ node_modules \\ express \\ lib \\ response.js:725:10)的ServerResponse.OutgoingMessage.setHeader(_http_outgoing.js:357:11) \\ O \\ OGPI6 \\ controllers \\ notes.js:9在ServerResponse.send(O:\\ OGPI6 \\ node_modules \\ express \\ lib \\ response.js:158:21)处的\\ express \\ lib \\ response.js:253:10) :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在setCursorDeadAndNotified(O:\\ OGPI6 \\ node_modules \\ mongodb-core \\ lib \\ cursor)的handleCallback(O:\\ OGPI6 \\ node_modules \\ mongodb-core \\ lib \\ cursor.js:171:5)处的\\ cursor.js:860:16。 js:505:3)

At my mind only error with "controller" in the callback function: 在我看来,回调函数中只有“ controller”错误:

if(err){
            console.log(err);
            return res.sendStatus(500);
        }
        res.send(docs);

But I thought that when an error occurs it must terminate function and return sendStatus(500) , but after logging an error in the console it tries to return res.send(docs) and then the app crashes because it is sending the second header. 但是我认为,当发生错误时,它必须终止函数并返回sendStatus(500) ,但是在控制台中记录错误后,它尝试返回res.send(docs) ,然后应用程序崩溃,因为它正在发送第二个标头。 It looks fine but doesn`t work. 看起来不错,但是不起作用。 Can anyone point which way I have failed? 谁能指出我失败的那条路?

Use the "next" parameter in the middleware to make express know that the purpose of this middleware is completed and there is no further need to execute the code. 使用中间件中的“ next”参数来明确表示该中间件的目的已完成,并且不再需要执行代码。

exports.all = function (req, res, next) {
    Notes.all(function(err, docs){
        if(err){
            console.log(err);
            res.sendStatus(500);
            return next();
        }
        res.send(docs);
    })
};

Execution of code after return maybe due to the async nature. 返回后执行代码可能是由于异步特性。

You can also use else block. 您也可以使用else块。

exports.all = function (req, res, next) {
        Notes.all(function(err, docs){
            if(err){
                console.log(err);
                res.sendStatus(500);

            }
            else res.send(docs);
        })
    };

Change the code to 将代码更改为

if(err){
  console.log(err);
  return res.status(500).send(err);
}
res.send(docs);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM