2

I am trying to give myself a practice to just use pure node as my server and PG to connect to PostgreSQL. What I want to do here is to return the result after querying from database and know it is problem of async. So how could I return the value from the callback function inside client.query.

function showAllMovies(){


pg.connect(connectionString, function (err, client, done) {
    if (err) {
      console.log('Can not log into database');
    } else {
      console.log('Connect to database...');
      client.query('SELECT * FROM movies', function (err, result) {
        done();  // client idles for 30 seconds before closing
        var result = JSON.stringify(result.rows[0].movie);
        console.log(result);
        return result;
      });
    }
  });
  pg.end();
}
S-Man
  • 22,521
  • 7
  • 40
  • 63
TotoLai
  • 53
  • 1
  • 4
  • Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Ben Fortune Dec 18 '15 at 08:41
  • Thanks for this information, @BenFortune , but I still don't know how to return the result value from this previous question. – TotoLai Dec 20 '15 at 07:19

1 Answers1

3

You can't return the value directly. You need to add a callback of your own to your function, which you will need to call, and provide the result to that callback.

For instance:

function showAllMovies(callback)
{
  pg.connect(connectionString, function (err, client, done)
  {
    if (err)
    {
      console.log('Can not log into database');
    }
    else
    {
      console.log('Connect to database...');
      client.query('SELECT * FROM movies', function (err, result)
      {
        callback(result.rows[0].movie);
        done();
      });
    }
  });
}

In this case, you can use it in your response handler like this:

showAllMovies(function(movie) { response.write('<li>'+movie+'</li>'); });

Note that this is just an example, you should actually do HTML escaping before output.

Alternatively, if you're responding to a request and using Express, you can simply pass down the response object and call one of its methods. For instance:

app.get('/movies', function(req,res)
{
  pg.connect(connectionString, function (err, client, done)
  {
    if (err)
    {
      console.log('Can not log into database');
    }
    else
    {
      console.log('Connect to database...');
      client.query('SELECT * FROM movies', function (err, result)
      {
        res.write('<li>'+result.rows[0].movie+'</li>');
        done();
      });
    }
  });
});

The same HTML escaping warning applies here.

jcaron
  • 17,302
  • 6
  • 32
  • 46
  • Thanks for this simple instruction, but I still stuck somehow. Because I will use this showAllMovies function in my requestHandler. Something like this: response.write('
  • ' + database.showAllMovies() + '
  • '); Could you give me some more guild line for this? thanks ! – TotoLai Dec 20 '15 at 07:24
  • Again, either you add a callback to your `showAllMovies` function, which will call it to do the output, or you pass down the `response`, and your function does the output. – jcaron Dec 20 '15 at 09:40
  • Edited the answer to illustrate how you can do it. – jcaron Dec 20 '15 at 09:44
  • Thank you very very very much! It's what I exactly need! I always thought how to 'return' the value back from showAllMovie function, but now I realize how much is the power of callback !! – TotoLai Dec 22 '15 at 07:15