0

I am trying to get the total records using node.js. I am facing issue in variable scope. Not sure how to handle this. My sample code is,

    var total_records = 0;
    var client = new pg.Client(pgConnect);
    client.connect();
    client.query(count_query, function(err, row) {
       total_records = row.rows[0]['count'];
        console.log(total_records);
    });
    console.log(total_records);

It prints right result in query block but print 0 after that block. I want to use its value after client.query block. Is it possible in Node.js?

Thanks in advance.

yasirnazir
  • 1,133
  • 7
  • 12
  • 2
    The query is asynchronous. That means the callback gets called sometime LATER, long after your last `console.log()` has already run. You can ONLY use the result of an asynchronous operation INSIDE the async callback. You can't use it outside of that because only inside the callback, do you have the result and know the timing of when the result is available. Welcome to node.js async programming. Lots of things in node.js work this way so you will need to learn how to program this way. – jfriend00 Jul 15 '16 at 21:38
  • @jfriend00 its not true that you can't use the log outside. you can use a library like http://caolan.github.io/async/ to easily achieve that. – aschmid00 Jul 15 '16 at 21:42
  • 1
    @aschmid00 - The async library just creates another callback and you have to use the result in that callback. You cannot use it outside the callback or some other function called from the callback unless you store it somewhere and then "guess" when the result might be done. That's asynchronous programming in node.js. Personally, I prefer to use promises for handling all my async development as they streamline a lot of this. – jfriend00 Jul 15 '16 at 21:51
  • @jfriend00 i agree in part. promises work the same way as callbacks at the end. what i dont like about promises is that first have to define a promise, then call `promise.resolve` which takes one single argument while callbacks (which i personally dont like at all) and also it does not through errors unless you define an error function. – aschmid00 Jul 15 '16 at 22:01
  • @jfriend00 I think http://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron/23667087#23667087 is a better duplicate for this kind of question. The answer there also links to the duplicate you chose, but the one you chose seems more specific to people expecting a return statement in a callback to return from the outer function. This one seems to be more about valriables modified inside the callback not being modified outside it. – Paul Jul 15 '16 at 22:02
  • @Paulpro - Yes, that's a good one for dup. There are hundreds to choose from. This is probably the most asked duplicate topic related to node.js on all of stack overflow. – jfriend00 Jul 15 '16 at 22:06
  • @aschmid00- If you create promisified versions of the async functions you use once in your app, then you can use them over and over without ever calling `promise.resolve()` yourself. They are substantially easier to use than plain callbacks, particularly when you have to coordinate or sequence multiple async operations and when you need to handle errors reliably in complex situations. I use the Bluebird promise library myself for [these reasons](http://stackoverflow.com/questions/34960886/are-there-still-reasons-to-use-promise-libraries-like-q-or-bluebird-now-that-we/34961040#34961040). – jfriend00 Jul 15 '16 at 22:08
  • @aschmid00 - Plus, this database library probably already has a promisified interface that can be used directly. The OP doesn't say what database it is, but most have an option now. – jfriend00 Jul 15 '16 at 22:10

0 Answers0