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:
我是node的新手。js(和mysql结合使用)并尝试基于请求参数和请求体更新数据库。我的文件开头是这样的:
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.
控制台。日志(mettmeister mettwochId);返回Jonas 1,没问题。
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! :)
如果我在我的数据库中手动执行查询,它会工作。我真的很高兴有任何帮助。谢谢你!:)
2 个解决方案
#1
3
Use update query like this
使用这样的更新查询
connection.query('UPDATE mettwoch SET ? WHERE ?', [{ mettmeister: mettmeister }, { mettwoch_id: mettwochId }])
#2
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();
#1
3
Use update query like this
使用这样的更新查询
connection.query('UPDATE mettwoch SET ? WHERE ?', [{ mettmeister: mettmeister }, { mettwoch_id: mettwochId }])
#2
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();