var connection = require('./../config');
var request = require("request");
module.exports.travelsummary = function (req, res, callback) {
var id = req.body.id;
var start_date = req.body.start_date;
var end_date = req.body.end_date;
var full_data = [];
if (id == undefined) {
res.json({
message: 'Please enter user id'
});
return;
}
var query1 = `SELECT imei FROM gs_user_objects WHERE user_id = ${id}`;
connection.query(query1, function (error, result1, fields) {
if (!error) {
for (i = 0; i < result1.length; i++) {
var table_name = 'gs_object_data_' + result1[i].imei;
console.log(table_name);
var query2 = `SELECT * FROM ${table_name} WHERE dt_server BETWEEN '${start_date}' AND '${end_date}'`;
//var qry3 = `SELECT * FROM gs_object_data_358899059206311 WHERE dt_server BETWEEN '2019-05-27 06:06:57' AND '2019-05-28 06:06:57'`
connection.query(query2, function (error, result2) {
full_data[i] = (result2);
console.log(full_data);
console.log(query2)
if (i == result1.length && !error && result2.length > 0) {
res.json({
message: 'data fetch successfuly',
data: full_data
})
}
});
}
}
});
};
从第一个查询响应开始作为内部查询的输入,然后将结果发送给响应,但数据仅获取最后一个表。
我如何获取每个表中的数据分开的数组?
输出-
{
"message": "data fetch successfully",
"data": [
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
[
{
"dt_server": "2019-05-27T00:42:16.000Z",
"dt_tracker": "2019-05-27T00:42:16.000Z",
"lat": 18.601397,
"lng": 73.932204,
"altitude": 0,
"angle": 0,
"speed": 0,
"params": "{\"gpslev\":\"5\",\"pump\":\"1\",\"track\":\"1\",\"bats\":\"1\",\"acc\":\"0\",\"defense\":\"0\",\"batl\":\"6\"}",
"ignition": "",
"power": "",
"AC": "",
"distance": "",
"total_distance": ""
},
{
"dt_server": "2019-05-27T00:49:46.000Z",
"dt_tracker": "2019-05-27T00:49:46.000Z",
"lat": 18.601397,
"lng": 73.932204,
"altitude": 0,
"angle": 0,
"speed": 0,
"params": "{\"gpslev\":\"5\",\"pump\":\"1\",\"track\":\"1\",\"bats\":\"1\",\"acc\":\"0\",\"defense\":\"0\",\"batl\":\"6\"}",
"ignition": "",
"power": "",
"AC": "",
"distance": "",
"total_distance": ""
},
{
"dt_server": "2019-05-27T01:08:03.000Z",
"dt_tracker": "2019-05-27T01:08:03.000Z",
"lat": 18.601397,
"lng": 73.932204,
"altitude": 0,
"angle": 0,
"speed": 0,
"params": "{\"gpslev\":\"5\",\"pump\":\"1\",\"track\":\"1\",\"bats\":\"1\",\"acc\":\"0\",\"defense\":\"0\",\"batl\":\"6\"}",
"ignition": "",
"power": "",
"AC": "",
"distance": "",
"total_distance": ""
},
{
"dt_server": "2019-05-27T01:17:50.000Z",
"dt_tracker": "2019-05-27T01:17:50.000Z",
"lat": 18.601397,
"lng": 73.932204,
"altitude": 0,
"angle": 0,
"speed": 0,
"params": "{\"gpslev\":\"5\",\"pump\":\"1\",\"track\":\"1\",\"bats\":\"1\",\"acc\":\"0\",\"defense\":\"0\",\"batl\":\"6\"}",
"ignition": "",
"power": "",
"AC": "",
"distance": "",
"total_distance": ""
},
{
"dt_server": "2019-05-27T01:25:11.000Z",
"dt_tracker": "2019-05-27T01:25:11.000Z",
"lat": 18.601397,
"lng": 73.932204,
"altitude": 0,
"angle": 0,
"speed": 0,
"params": "{\"gpslev\":\"5\",\"pump\":\"1\",\"track\":\"1\",\"bats\":\"1\",\"acc\":\"0\",\"defense\":\"0\",\"batl\":\"6\"}",
"ignition": "",
"power": "",
"AC": "",
"distance": "",
"total_distance": ""
},
{
"dt_server": "2019-05-27T01:32:31.000Z",
"dt_tracker": "2019-05-27T01:32:31.000Z",
"lat": 18.601397,
"lng": 73.932204,
"altitude": 0,
"angle": 0,
"speed": 0,
"params": "{\"gpslev\":\"5\",\"pump\":\"1\",\"track\":\"1\",\"bats\":\"1\",\"acc\":\"0\",\"defense\":\"0\",\"batl\":\"6\"}",
"ignition": "",
"power": "",
"AC": "",
"distance": "",
"total_distance": ""
}
]
]
}
我正在尝试从该表从第一个查询获得的表中搜索数据,然后从每个给出第一个查询的表中搜索数据,以便为什么使用嵌套查询。 在输出中,将所有数据作为null推送,最后一个表数据发送到数组中。
答案 0 :(得分:0)
在先前查询的响应之前,已收到数据库对最后一个查询/表的响应。 最好使用promise:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all
答案 1 :(得分:0)
Bro,您需要使用async-await或promise
例如:代码
var connection = require('./../config');
var request = require("request");
module.exports.travelsummary = async function (req, res, callback) {
try {
var id = req.body.id;
var start_date = req.body.start_date;
var end_date = req.body.end_date;
var full_data = [];
if (id == undefined) {
res.json({
message: 'Please enter user id'
});
return;
}
let query1 = `SELECT imei FROM gs_user_objects WHERE user_id = ${id}`;
let result1 = await connection.query(query1);
if (result1 && result1.length) {
await Promise.all(result1.map((item, i) => {
let table_name = 'gs_object_data_' + item.imei;
let query2 = `SELECT * FROM ${table_name} WHERE dt_server BETWEEN '${start_date}' AND '${end_date}'`;
let result2 = await connection.query(query2);
full_data.push(result2);
}))
res.json({
message: 'data fetch successfuly',
data: full_data
})
} else
return "empty"
} catch (error) {
return error
}
};
为了使用异步等待,您需要使用promise-MySQL npm创建连接。它只是一个包装器。