简体   繁体   English

Node.JS中的MySQL请求始终返回旧数据集

[英]MySQL request in Node.JS always returns old data set

I am new to Node.JS, so probably, it's a trivial question, but I spent hours on it and haven't found any solution. 我是Node.JS的新手,所以这可能是一个微不足道的问题,但是我花了几个小时没有找到任何解决方案。 The problem. 问题。 MySQL query in Node.JS always returns the same data set since the startup of the server, no matter how I change the database. 自服务器启动以来,无论我如何更改数据库,Node.JS中的MySQL查询始终返回相同的数据集。 After I restart the server, it starts returning the correct data, untl I update them again. 重新启动服务器后,它将开始返回正确的数据,直到我再次对其进行更新。 IE, data set never changes since the start up. IE,数据集自启动以来从未改变。

POST updates the table, GET gets the data set, but always the old data. POST更新表,GET获取数据集,但始终获取旧数据。 Tried to update via POST and via MySQL workbench, it's always the same. 试图通过POST和MySQL工作台进行更新,它始终是相同的。

I thought, it's because I didn't end the connection. 我以为是因为我没有结束连接。 After some research, I added con.end() line after each query, but still of no help 经过研究,我在每个查询后添加了con.end()行,但仍然没有帮助

This is my db_connector.js module: 这是我的db_connector.js模块:

var mysql = require('mysql');

module.exports = {
resSet: function(callback) {
    var con = mysql.createConnection({
    host: "localhost",
    user: "root",
    password: "pass",
    database: "applocator"
    });  
    con.query("SELECT * FROM locations",
    function (err, rows) {
    //here we return the results of the query
    callback(err, rows); 
      }
      );
    con.end();
    },
    resUpd: function(id, lat, long){
    var con = mysql.createConnection({
        host: "localhost",
        user: "root",
        password: "pass",
        database: "applocator"
      });  

      con.connect(function(err) {
        if (err) throw err;
        var sql = "UPDATE locations SET latitude = "+lat+", longitude = 
    "+long+" WHERE id = "+id;
        console.log(sql);
        con.query(sql, function (err, result) {
          if (err) throw err;
          console.log(result.affectedRows + " record(s) updated");
        });
      });    
      con.end();      
    }
}

It exports two functions, one to get the data set, one to update it. 它导出两个函数,一个用于获取数据集,一个用于更新数据集。

I use it in the server.js this way: 我以这种方式在server.js中使用它:

var app = express();

app.use(allowCrossDomain); 
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

app.post('/',function(req,res){
query.resUpd(req.body.id,req.body.lat,req.body.long)
console.log(req.body);
res.send("Success");
});

query.resSet(function (err, myRes){

app.get('*', (req, res) => 
res.status(200).send(JSON.stringify(myRes)));
})


app.listen(3000);

I must get the updated data set after I make an update, but am always getting the old data set, like I have not made any updates to the table. 更新后,我必须获取更新的数据集,但是总是获取旧的数据集,就像我没有对表进行任何更新一样。 What is my problem? 我怎么了 I'll be very grateful for your responses. 谢谢您的回复。

The server returns the stale data because you register app.get('*' ...) handler inside the callback that is triggered by mysql select query. 服务器返回过时的数据,因为您在由mysql select查询触发的回调中注册了app.get('*' ...)处理程序。 It should be vice versa, so in server.js try to change: 反之亦然,因此在server.js尝试更改:

query.resSet(function (err, myRes){
  app.get('*', (req, res) =>
    res.status(200).send(JSON.stringify(myRes)));
})

into 进入

app.get('*', (req, res) => {
  query.resSet(function (err, myRes) {
    res.status(200).send(JSON.stringify(myRes));
  })
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM