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
});
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…