Cannot set headers after they are sent to the client

D:\le\node_modules\mysql\lib\protocol\Parser.js:80
        throw err; // Rethrow non-MySQL errors
        ^

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the cli
ent
    at ServerResponse.setHeader (_http_outgoing.js:470:11)
    at ServerResponse.header (D:\le\node_modules\express\lib\response.js:767:
10)
    at ServerResponse.send (D:\le\node_modules\express\lib\response.js:170:12
)
    at ServerResponse.json (D:\le\node_modules\express\lib\response.js:267:15
)
    at ServerResponse.send (D:\le\node_modules\express\lib\response.js:158:21
)
    at D:\le\routes\cart.js:23:25
    at D:\le\models\cart.js:16:14
    at Query._callback (D:\le\models\db.js:44:22)
    at Query.Sequence.end (D:\le\node_modules\mysql\lib\protocol\sequences\Se
quence.js:88:24)
    at Query.ErrorPacket (D:\le\node_modules\mysql\lib\protocol\sequences\Que
ry.js:90:8)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! letao@0.0.0 start: `node ./bin/www`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the letao@0.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional log
ging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\administrator\AppData\Roaming\npm-cache\_logs\2018-11-28T11_00_46_4
81Z-debug.log

D:\letao>

注意其中at Query._callback (D:\le\models\db.js:44:22)找到models\db.js44行

    pool.getConnection(function(err, connection) {
        if (err) {
            // callback(err);
            return callback(err);
        }
        connection.query(sqlStr, params, function(err, rows) {
            if (err) {
                // callback(err);
                return callback(err);
            }
            connection.release();
            callback.apply(null, arguments);//报错行44
        });
    });

在callback(err)前加return上述错误解决了。具体原因详见参考文章。

参考文章:

https://stackoverflow.com/questions/7042340/error-cant-set-headers-after-they-are-sent-to-the-client
https://cnodejs.org/topic/53774ffecbcc396349ca1155
https://cnodejs.org/topic/5635fed78c67728402553324
https://stackoverflow.com/questions/27658997/cant-set-headers-after-they-are-sent-on-express

 

posted @ 2018-11-29 20:01  小方块的世界  阅读(14216)  评论(0编辑  收藏  举报