3

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! :)

18
  • When the server doesn't respond, does the value in DB get updated? Commented Apr 5, 2015 at 9:25
  • Is the mettwoch_id an INT? If so, try removing quotes: mettwoch_id = 1 Commented Apr 5, 2015 at 9:26
  • You should escape your variables using in sql because your code may be vulnerable to sql injection. Commented Apr 5, 2015 at 9:26
  • @Mr.Web It is but executing it manually works fine. Commented Apr 5, 2015 at 9:28
  • @jonicious, I see... Commented Apr 5, 2015 at 9:29

2 Answers 2

4

Use update query like this

connection.query('UPDATE mettwoch SET ? WHERE ?', [{ mettmeister: mettmeister }, { mettwoch_id: mettwochId }])
Sign up to request clarification or add additional context in comments.

1 Comment

My code now looks like this: connection.query('UPDATE mettwoch SET ? WHERE ?', [{ mettmeister: mettmeister }, { mettwoch_id: mettwochId }], function(err) { console.log(err); if (!err) { res.status(201); } else { res.status(500); } });
1

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();

3 Comments

That is correct, but you have been telling that DB is also not getting updated.
@thefourtheye Overlooking the obvious things.. :/ I am so sorry :(
If I am not mistaken, shorter way -- sendStatus.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.