0

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!

3
  • One missing thing, at least, is sql3 parameters replace: sql3 = mysql.format(sql3, sensor); Commented Mar 14, 2019 at 12:43
  • There is no reason the script crashes at var pTime = res[i].timestamp; if it can read var sensor = res[i].sensor_id; try rewriting the line, a unexpected character may have popped before ` res[i]`, an irregular white-space for example Commented Mar 14, 2019 at 13:04
  • I edited the code in question. There was actually a var sTime = res[i].timestamp; in the second query. Commented Mar 14, 2019 at 13:13

2 Answers 2

3

The problem is actually not really a problem. when you have [object Object] in any console.log, it means that what you want to print in a string is not possible to print as a string.

If you really want to print the result, what you should do instead of

conn.query(sql2, function (err, results, fields) {
    if (err) throw err;
    console.log("results = " + results + " " + results.length); // HERE
    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); // HERE
    });
});

Is actually :

conn.query(sql2, function (err, results, fields) {
    if (err) throw err;
    console.log("results ="); // HERE
    console.log({ results }, results.length); // HERE
    conn.query(sql3, function (err, res, fields) {
        if (err) throw err;
        var location = [res[i].latitude, res[i].longitude];
        console.log("Data = "); // HERE
        console.log({ sensor, pTime, sTime, latitude, longitude }); // HERE
    });
});

See the notation console.log({ variable }); This allows you to log your variable content, with the key displayed on the left, as you'd actually want to do in your current console.logs.

Example :


const results = [1, 2, 3, 4];
const randomString = 'Some content in here';

console.log({ results, randomString});

This outputs : enter image description here

Sign up to request clarification or add additional context in comments.

7 Comments

See my comment on your post concerning the Exception @nelaaam
Oh I was able to see that I get the results. But how do I store it to a variable? It still throws TypeError: Cannot read property 'latitude' of undefined. Is my way of getting the data (res[i].latitude) incorrect?
Look at the params of the function. Your res reference is no longer the one you are iterating on, but the variable you have in your function callback. (at conn.query(sql3, function (err, res, fields) {)
I already tried changing the variable name but it still gives me the error.
Then it is probably a scope problem. Try storing your first callback parameter in a variable before entering in another callback.
|
1

You can't console combination of string and object at once time.

Try below format, It might help you.

console.log('results =');
console.log(results);
console.log(results.length);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.