3

I have this simple code

 orm: function (req, res) {

    // Send a JSON response


       Noder.query('SELECT * FROM crud ', function(err, results) {
        var all_rows = Noder.query('SELECT count(*) from crud ', function(err, the_rows) {
        return the_rows;
        });

         res.view('noder/orm', {
            layout: 'layout',
            allr:all_rows,
            post:results,
            title: 'This is the hi page title. '
        }); 
      });
   },

which i am using to fetch all rows in a mysql table. However inside that function,i want to have another function that counts how many rows there are in the table.My variable var all_rows shows me undefined when i try displaying it. How can i solve this?.

1
  • 2
    Do not return the_rows, put res.view('noder/orm', { layout: 'layout', allr:all_rows, post:results, title: 'This is the hi page title. ' }); inside Noder.query success callback Commented Dec 25, 2015 at 17:14

1 Answer 1

1

This is because you are accessing the value of all_rows before the inner-query has returned.

Noder.query is an asynchronous function, and as such, its execution will be delayed until the query itself is completed. Meanwhile, your orm function will continue merrily down and call res.view while your inner query is still processing.

To fix this, you can call res.view from inside your inner query.

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

Comments

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.