0

Naive to NodeJS and trying to figure out a way to add results from second table to result set of first table in node js.

How can I access results from first query inside the second query?

Following is my code snippet with comments

function getTasks(callback) {
//first query gives result set
connection.query('SELECT * FROM ' + properties.get('database.Table') +' order by timestamp desc', function(err, rows){
    if(!err){
        //for each result from result set, match on Id and get values from table_2
        for (var i = rows.length - 1; i >= 0; i--) {
            connection.query('SELECT * FROM table_2 where taskId = "' + rows[i].taskId + '"', function(err, sets){
                if(!err){
                    //if we have any results from table_2 then create an object
                    if(sets.length > 0){
                        var setStatus = [];
                        for (var i = sets.length - 1; i >= 0; i--) {
                            setStatus[i] = {Status : sets[i].type+'-'+sets[i].status};
                        }
                        //add the setStaus object to results from first table (to rows)
                        //ISSUE: accessing rows[i] here is alwyas undefined??
                    }
                }
            });
        }
        //need to send the rows with updates from nested block
        callback(rows);
    }
});

UPDATE: async/await solution worked and by changing i to j for inner iterator!

1
  • use async/await for async call. Commented Feb 20, 2018 at 17:11

3 Answers 3

2

You can handle using async/await

const execute = (query) => {
    return new Promise((resove, reject) => {
        connection.query(query,
            function (err, rows) {
                if (err) reject(err);
                resove(rows);
            });
    });
}

const getTasks = async () => {
    const query = 'SELECT * FROM ' + properties.get('database.Table') + ' order by timestamp desc';
    const rows = await execute(query);
    for (var i = rows.length - 1; i >= 0; i--) {
        const innerQuery = 'SELECT * FROM table_2 where taskId = "' + rows[i].taskId + '"';
        const sets = await execute(innerQuery);
        //Do some stuff
        if (sets.length > 0) {
            var setStatus = [];
            for (var i = sets.length - 1; i >= 0; i--) {
                setStatus[i] = {
                    Status: sets[i].type + '-' + sets[i].status
                };
            }
        }
    }
    return rows;
};

Yau can call either inside await or given below

getTasks().then((rows) => {
    console.log(rows);
}).catch((err) => {
    console.log(err);
})
Sign up to request clarification or add additional context in comments.

Comments

1

The declaration of i in the second for loop overwrites the value of i from the first loop.

You can fix this by changing the variable declared as part of the second loop to something other than i.

Comments

1

Try to use let or const instead of var. Use a different variable name than i for all your iterators. This is probably messing up with your first i, thus leading to rows[i] as undefined.

You also probably want to look for a MySQL node module that supports ES6 Promises like this one : https://www.npmjs.com/package/mysql2

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.