I am new to node.js (and mysql in combination 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
{ command: 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! :)
mettwoch_idan INT? If so, try removing quotes:mettwoch_id = 1