Express.js存储来自响应DB的数据

时间:2017-02-03 18:25:59

标签: javascript node.js express

我有node / express.js的问题 我连接到数据库,我正在从数据库中恢复数据,并将其作为res.send保存到“localhost:3000 / interval”。我想将它存储到变量中,因此它在./interval中无法查看,解析它,并传递给谷歌图表API绘制图表:)到目前为止,我没有被困住这么久。请帮助:)

来自DB的数据

[{"CZAS":"2017-02-02 21:19:05","PM1":"28","PM25":"40","PM10":"48","ID":"300"},{"CZAS":"2017-02-03 07:17:07","PM1":"45","PM25":"69","PM10":"86","ID":"600"},{"CZAS":"2017-02-03 07:25:58","PM1":"67","PM25":"110","PM10":"139","ID":"900"},{"CZAS":"2017-02-03 07:35:11","PM1":"72","PM25":"121","PM10":"164","ID":"1200"}

这是来自server.js

app.get('/interval', common.listPoints2);

常见代码。

exports.listPoints2 = function(req, res) {
var data = require('../data.js');
console.log("Getting list points " + req + " res " + res);
data.connect();
console.log("Conecnted ...");
data.fiveMinInterval(1, res);}

这是一个功能

exports.fiveMinInterval = function(req, res) {
var xys = [];
console.log("Get data from DB");
client.query('SELECT time AS CZAS, pm1 AS PM1, pm25 AS PM25, pm10 AS PM10, id AS ID FROM dev_0_data WHERE id%300=0')
.on('result', function(res) {

        res.on('data', function(row) {
    xys.push(row);
        })
    .on('error', function(err) {
                console.log('Result error: ' + inspect(err));
    })
    .on('end', function(info) {
                console.log('Result finished successfully');
    });
})
.on('end', function() {
    console.log("Get track points end");

      res.send(xys);
});

}

1 个答案:

答案 0 :(得分:0)

打印“获取跟踪点”?

请注意,您的on result回调函数参数和您的fiveMinInterval参数“res”具有相同的变量名称。