Yodha
Yodha

Reputation: 45

Nested queries - undefined error

I have written a function and exported it using exports. Inside the function I have 2 nested queries. When I have only 1 query I am able to view the values of result. But, after adding the outer query the value of the result from the outer query is undefined.

Here is the code: main.js

var new1 = function () {
  connection.query("select ID from tbl1", function (error, result, fields) {
    console.log(result) // This is displayed as undefined.
    for (var id in result) {
      connection.query("select name from tbl2 where ID = '" + result[id].ID + "' ", function (err, result, fields) {
        if (err) throw err

        for (var count in result) {
          console.log(result[count].name)
        }
      })
    }
  })
}
export.new1 = new1;

and in the app.js:

  var new2 = require('./main');
  new2.new1();

Thank you.

Upvotes: 0

Views: 46

Answers (1)

Johan Karlsson
Johan Karlsson

Reputation: 494

I was going to edit and fix your indentation, but then I saw you are missing a closing quote for the string on the second line: "select ID from tbl1

Now someone else have edited your code and added a closing quote

Upvotes: 1

Related Questions