I am new to node.js (and mysql in bination with that) and trying to update my database based on request parameter and a request body. My beginning of the file looks like this:
var express = require('express');
var bodyParser = require('body-parser');
var mysql = require('mysql');
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'root',
database: 'm3ttulat0r',
debug: true
});
var app = express();
app.use(bodyParser.json());
My request looks like this:
http://localhost:8080/mettmeister/1
The request body looks like this:
{
"mettmeister": "Jonas"
}
The connection to the database is successful. Then I have the following code:
app.post('/mettmeister/:mettwochId', function(req, res) {
var mettmeister = req.body.mettmeister;
var mettwochId = req.params.mettwochId;
var query = 'UPDATE mettwoch SET mettmeister = "'+ mettmeister +'" WHERE mettwoch_id = "'+ mettwochId +'"';
console.log(mettmeister, mettwochId);
connection.query(query, function(err, result) {
if (!err) {
res.status(201);
} else {
res.status(500);
}
});
});
console.log(mettmeister, mettwochId);
returns Jonas 1
which is fine.
The problem is that it sends the request and the server gets the message according to the log but nothing happens. The server "stops" at executing the query apparently.
I have debugging turned on and the query looks fine.
--> ComQueryPacket
{ mand: 3,
sql: 'UPDATE mettwoch SET mettmeister = "Jonas" WHERE mettwoch_id = "1"' }
If I execute the query manually in my database, it works.. I am really happy for any help. Thank you! :)
I am new to node.js (and mysql in bination with that) and trying to update my database based on request parameter and a request body. My beginning of the file looks like this:
var express = require('express');
var bodyParser = require('body-parser');
var mysql = require('mysql');
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'root',
database: 'm3ttulat0r',
debug: true
});
var app = express();
app.use(bodyParser.json());
My request looks like this:
http://localhost:8080/mettmeister/1
The request body looks like this:
{
"mettmeister": "Jonas"
}
The connection to the database is successful. Then I have the following code:
app.post('/mettmeister/:mettwochId', function(req, res) {
var mettmeister = req.body.mettmeister;
var mettwochId = req.params.mettwochId;
var query = 'UPDATE mettwoch SET mettmeister = "'+ mettmeister +'" WHERE mettwoch_id = "'+ mettwochId +'"';
console.log(mettmeister, mettwochId);
connection.query(query, function(err, result) {
if (!err) {
res.status(201);
} else {
res.status(500);
}
});
});
console.log(mettmeister, mettwochId);
returns Jonas 1
which is fine.
The problem is that it sends the request and the server gets the message according to the log but nothing happens. The server "stops" at executing the query apparently.
I have debugging turned on and the query looks fine.
--> ComQueryPacket
{ mand: 3,
sql: 'UPDATE mettwoch SET mettmeister = "Jonas" WHERE mettwoch_id = "1"' }
If I execute the query manually in my database, it works.. I am really happy for any help. Thank you! :)
Share Improve this question asked Apr 5, 2015 at 9:23 joniciousjonicious 3641 gold badge4 silver badges15 bronze badges 18- When the server doesn't respond, does the value in DB get updated? – thefourtheye Commented Apr 5, 2015 at 9:25
-
Is the
mettwoch_id
an INT? If so, try removing quotes:mettwoch_id = 1
– Mr.Web Commented Apr 5, 2015 at 9:26 - You should escape your variables using in sql because your code may be vulnerable to sql injection. – jcubic Commented Apr 5, 2015 at 9:26
- @Mr.Web It is but executing it manually works fine. – jonicious Commented Apr 5, 2015 at 9:28
- @jonicious, I see... – Mr.Web Commented Apr 5, 2015 at 9:29
2 Answers
Reset to default 4Use update query like this
connection.query('UPDATE mettwoch SET ? WHERE ?', [{ mettmeister: mettmeister }, { mettwoch_id: mettwochId }])
Well, I am stupid. Sorry guys :(
I did the following
res.status(201);
Actually you have to finish the request like this:
res.status(201).end();