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();
}
2
  • Possible duplicate of How do I return the response from an asynchronous call? Commented Dec 18, 2015 at 8:41
  • Thanks for this information, @BenFortune , but I still don't know how to return the result value from this previous question. Commented Dec 20, 2015 at 7:19

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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('<li>' + database.showAllMovies() + '</li>'); Could you give me some more guild line for this? thanks !
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.
Edited the answer to illustrate how you can do it.
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 !!

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.