4

Why is resultofgetCauses undefined? I'm not sure if the function is not returning currentCauses or if it not being assigned to resultofgetCauses . . . or if this has something to do with asynchronicity.

function getCauses(){

  var currentCauses;
  client = pg.connect(connectionString, function(err, client, done){

    if(err) console.log(err);

    client.query('SELECT * FROM causes', function(err, result){
      //console.log(result.rows);
      console.log('poo');
      currentCauses=result.rows;
      //console.log(currentCauses);
    });

  });

  return currentCauses;
};

var resultofgetCauses = getCauses();
5
  • 3
    Asynchronous code? See here stackoverflow.com/questions/14220321/… Commented Oct 28, 2013 at 23:08
  • I don't think it's the asynchronicity that's the problem though Commented Oct 28, 2013 at 23:14
  • 1
    It is, currentCauses is undefined as resultofgetCauses gets assigned before pg.connect has time to finish. That question has an in-depth solution to your problem. Commented Oct 28, 2013 at 23:16
  • 1
    There are actually 2(nested) async calls here. pg.connect and client.query Commented Oct 28, 2013 at 23:18
  • This question is similar to: How do I return the response from an asynchronous call?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Dec 8, 2024 at 6:49

2 Answers 2

4

Yes since it's running asynchronously, by the time result.rows is assigned to the currentCauses variable, the line return currentCauses has already been executed thus the value is undefined.

You may want to do that as follow

var resulttofgetCauses;
function getCauses(){
    var currentCauses;
    client = pg.connect(connectionString, function(err, client, done){
        if(err) console.log(err);
        client.query('SELECT * FROM causes', function(err, result){
            //console.log(result.rows);
            console.log('poo');
            currentCauses=result.rows;
            //console.log(currentCauses);
            resulttofgetCauses = currentCauses;
        });
    });
};
getCauses();

To be more specific with the answer, executing 'SELECT * FROM causes' sql does not give you the result right after its execution time. It takes at least 0.00something seconds to retrieve the data from database. So in the very short time between 'executing sql' and 'receiving requested data' JavaScript has already executed return currentCauses; while the correct data is still in the progress of retrieving. Because it's async and won't wait. There are good example code out there on the internet you may want to check out.

Plus It's a good practice to define function as follow

getCauses = function () { ... }
Sign up to request clarification or add additional context in comments.

Comments

0

Because currentCauses is initialized in an async invoked function (the function passed as param to pg.connect) after you have already returned from getCauses function. By the time you return the value of currentCauses is undefined.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.