Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
146 views
in Technique[技术] by (71.8m points)

node.js - Why? Error: Can't set headers after they are sent

I keep getting this error: Error: Can't set headers after they are sent.

I have read other posts but I don't understand. I dont have any double callbacks or anything. Where in my code is causing this error?

Error: Can't set headers after they are sent.
    at ServerResponse.OutgoingMessage.setHeader (http.js:689:11)
    at ServerResponse.header (/root/node_modules/express/lib/response.js:666:10)
    at ServerResponse.send (/root/node_modules/express/lib/response.js:146:12)
    at fn (/root/node_modules/express/lib/response.js:900:10)
    at View.exports.renderFile [as engine] (/root/node_modules/jade/lib/jade.js:330:12)
    at View.render (/root/node_modules/express/lib/view.js:93:8)
    at EventEmitter.app.render (/root/node_modules/express/lib/application.js:530:10)
    at ServerResponse.res.render (/root/node_modules/express/lib/response.js:904:7)
    at Query. (/root/tutsplus/server4.js:25:7)
    at Query.emit (events.js:98:17)
var express = require('express'), app = express();
var mysql   = require('mysql');

app.get('/', function(req,res) {

    var connection = mysql.createConnection({
        host: 'localhost',
        user: 'root',
        password: 'xxxx',
        database: 'store'
    });

    var query = connection.query('SELECT * from category');

    query.on('result', function(row) {

        var category = row.category_name;

        res.render('xxxx.jade', {
            category: category
        });
    });

}); // app.get

app.listen(80, function() {

    console.log('we are logged in');
});
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As I said in my comment, this issue is nearly always caused by improper handling of asynchronous operations which causes pieces of the response to be called out of order.

Per the code example here that uses .on(), you need to end the request only when you get:

query.on('end', function() {
     // all rows have been received
});

I think you are probably calling res.render() more than once because you're calling it in query.on('result', ...) rather than in query.on('end', ....) after all the data has been collected.

The fact that you're doing it in:

query.on('result', ...)

is probably the wrong timing issue causing the problem.


From the mysql nodejs connector documentation, here's an example:

var query = connection.query('SELECT * FROM posts');
query
  .on('error', function(err) {
    // Handle error, an 'end' event will be emitted after this as well
  })
  .on('fields', function(fields) {
    // the field packets for the rows to follow
  })
  .on('result', function(row) {
    // Pausing the connnection is useful if your processing involves I/O
    connection.pause();

    processRow(row, function() {
      connection.resume();
    });
  })
  .on('end', function() {
    // all rows have been received
  });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...