I have this code:
var sql1 = "SELECT DISTINCT sensor_id, timestamp FROM Displacement_Record WHERE wave_type = 1 ORDER BY timestamp ASC";
var sql2 = "SELECT timestamp FROM Displacement_Record WHERE wave_type = 0 AND sensor_id = ?";
var sql3 = "SELECT latitude, longitude FROM Sensor_Record WHERE sensor_id = ?";
var pTime, sTime;
db.getConnection((err, conn) => {
if (err) throw err;
conn.query(sql1, function (err, res, fields) {
if (err) throw err;
for (i = 0; i < res.length; i++) {
var sensor = res[i].sensor_id;
console.log(sensor);
var pTime = res[i].timestamp;
sql2 = mysql.format(sql2, sensor);
conn.query(sql2, function (err, results, fields) {
if (err) throw err;
var sTime = res[i].timestamp; //timestamp error
console.log("results = " + results + " " + results.length);
conn.query(sql3, function (err, res, fields) {
if (err) throw err;
var location = [res[i].latitude, res[i].longitude];
console.log("Data = " + sensor + " " + pTime + " " + sTime + " " + latitude + " " + longitude);
});
});
}
});
conn.release();
});
The first query give me the results properly. I am able to get and store the data I need. However, the second and third query results as:
results = [object Object],[object Object],[object Object]
and throws:
throw err; // Rethrow non-MySQL errors
^
TypeError: Cannot read property 'timestamp' of undefined
I tried to see what causes this. I am able to get a proper result only when I remove the condition in the query. However, I need the condition of course. And I'm also wondering how come it throws error in the following queries when I am able to get results on the first query, with conditions and all.
I hope somebody can help me, thanks!

sql3 = mysql.format(sql3, sensor);var pTime = res[i].timestamp;if it can readvar sensor = res[i].sensor_id;try rewriting the line, a unexpected character may have popped before ` res[i]`, an irregular white-space for examplevar sTime = res[i].timestamp;in the second query.