Reputation: 1432
I'll keep the code simple, but basically, I have a Node.js server running Express. Express is connected to a MySQL database using pooling (and the mysql npm package). This is what my server looks like (I left out the boring requires and whatnot to keep this simple). This is the only routing that the server handles.
server.all('/test', function (req, res, next) {
pool.getConnection(function (err, conn) {
if (err) {
console.log(err);
}
else {
conn.query("select * from spwp_appusers where id=46", function (err, rows) {
conn.release();
if (err) {
console.log(err);
}
else {
res.send(200);
}
});
}
});
next();
});
However, when I run this code, the server tries to execute res.send(200);
but breaks and I get the following error:
/usr/lib/node_modules/mysql/lib/protocol/Parser.js:77
throw err; // Rethrow non-MySQL errors
^
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:335:11)
Does anyone know what is happening? Why can't I send the response? Even when I use a callback, I get this error.
Upvotes: 1
Views: 3917
Reputation: 83
I think the problem is with next. try changing next();
inside getConnection()
Upvotes: 3
Reputation: 1555
above error show in my case mismatch my mysql user,password,db name so please check your mysql credential.
var pool = mysql.createPool({ host: 'localhost', user: 'root', password: '', database: 'dbname', multipleStatements: true });
Upvotes: 0