0

I am attempting to get data from a mysql table using nodejs. I call the sql routine which is in another node js file. However, I cannot get my callback to return the data. I think the problem might be that I am calling an asynchronous routine from a node js file rather than from an angular or regular javascript program. Below is the exports script I am calling.

exports.getVenueById = function(db, id) {
    var http = require('http');
    var mysql = require('mysql');

    var query = "SELECT *  FROM venues WHERE auto_increment = ? "
    query = mysql.format(query, id);
    console.log("query=" + query);

db.query(
    query,
    function(err, rows) {
        if(err) console.log("error=" + err);
        console.log("rows=" + rows[0]['auto_increment']);
        res.json({success: true, response: rows});
        //return rows[0];
     });

}

I know this is working because I am writing the results to the console. The problem is the data never gets back to the calling node js routine below.

function getVenueData (id , callback)  {             
  (function () {
     venueData = venueTable.getVenueById(db, id);           
     if(venueData) {
         callback();
         console.log("callback in loop");
      }
      console.log("callback after loop");        
  });        
}

getVenueData(id, gotVenueData);

The program immediately returns and displays the timeout message. I admit that I am not an expert in nodejs or its callback feature. I would appreciate any feedback as to how I can get the program to wait for the asynchronous data to return to the calling program.

function gotVenueData() {
  console.log("gotVenueData");
}

setTimeout(function() { console.log("timeout for 10 seconds");} , 10000);
console.log("venuedata=" + venueData);
3
  • It's not considered as answer, but there's a typo in your first code part. You're missing a semicolon behind var query = "SELECT * FROM venues WHERE auto_increment = ? " It's also missing in the answer of @SomeKittens Commented Mar 27, 2014 at 1:34
  • @morten.c odd, didn't get pinged there. Fixing it in my answer. It's not too serious of a typo, as JS will automatically add it. Commented Mar 27, 2014 at 1:38
  • Thanks for finding the typo. Commented Mar 27, 2014 at 2:14

1 Answer 1

5

You're trying to return async data syncronously. Instead, add a callback to getVenueById:

exports.getVenueById = function (db, id, cb) { var http = require('http'); var mysql = require('mysql');

var query = "SELECT *  FROM venues WHERE auto_increment = ? ";
query = mysql.format(query, id);
console.log("query=" + query);

db.query(
query,

function (err, rows) {
    if (err) console.log("error=" + err);
    console.log("rows=" + rows[0]['auto_increment']);
    cb({
        success: true,
        response: rows
    });
    //  return rows[0];
});

and use it as such:

venueTable.getVenueById(db, id, function(data) {
  console.log(data);
});

One caveat: Traditionally the callback has the first parameter as the error (or null) and then the actual data. With that in mind:

exports.getVenueById = function (db, id, cb) { var http = require('http'); var mysql = require('mysql');

var query = "SELECT *  FROM venues WHERE auto_increment = ? ";
query = mysql.format(query, id);
console.log("query=" + query);

db.query(
query,

function (err, rows) {
    if (err) { cb(err); }
    console.log("rows=" + rows[0]['auto_increment']);
    cb(null, {
        success: true,
        response: rows
    });
    //  return rows[0];
});

and use it as such:

venueTable.getVenueById(db, id, function(err, data) {
  if (err) { return console.log(err); }
  console.log(data);
});
Sign up to request clarification or add additional context in comments.

2 Comments

Wow! Thanks for educating me in the world of callbacks. I applied your changes and data is coming back. I need to brush up more on callbacks as that is the power of nodejs. I scoured the internet trying to find an example of what I wanted to do and you are the one who showed me. Thanks a million!
How do you get the correct last_insert_id with an async mysql insert query?

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.