0

This is an Express site.

Here is how I had my code. This is just some spiked code for me to play with to try to get something dirty working:

index/routes.js

    router.get('/', function(req, res) {
        var sqlStatement = "Select top 10 from customers";

        var rows = connection.invokeQuery(sqlStatement);

        res.render('index', { title: 'My App', customers: rows});
    });

module.exports = router;

The problem is it's not waiting for the query to complete before trying to render the data to the page. So I tried putting it in a callback, but still doesn't work. Do I need a promise or watcher or something?

router.get('/', function(req, res) {
    var sqlStatement = "Select top 10 from customers";

    var rows = connection.invokeQuery(sqlStatement, function(){
            res.render('index', { title: 'My App', customers: rows});
    });
});

I created this method also:

connection.invokeQuery = function(sqlQuery){

...code before this

                db.query(sqlQuery, function(err, rows){

                    if(rows ) {
                        data = rows;
                        console.log(rows);

                    }
                    if(err) { console.log(err); }
                })
            }
        );
    });

    return data;
};

Another thing I noticed is that in my invokeQuery function when I fire up my site, it's hitting the return data right off the bat. It doesn't even hit my sql query yet. So that's another problem.

I'm probably going about this completely wrong but I don't get how to handle this probably very common scenario.

ANSWER

Ok this is easy, figured it out.

var rows = connection.invokeQuery(sqlStatement, function(rows){
    res.render('index', { title: 'Dave\'s App', cobrands: rows});
});

connection.invokeQuery = function(sqlQuery, callback){

...code before this

                db.query(sqlQuery, function(err, rows){

                    if(rows ) {
                        data = rows;
                        console.log(rows);

                        callback(rows);
                    }
                    if(err) { console.log(err); }
                })
            }
        );
    });
PositiveGuy
  • 17,621
  • 26
  • 79
  • 138
  • just gotta love stack :P. There will always be duplicates. You will never be able to control this...no matter how much you ask people to "search" for existing...it's not always findable or examples that seem repeats are often very specific to unique code or situations half the time. – PositiveGuy Jun 08 '15 at 04:43
  • so it's a dup, what you going to do delete it? christ. – PositiveGuy Jun 08 '15 at 04:45
  • oh btw that example dup post sucks. It doesn't show the implementation of the jquery .ajax method which is far more useful and important to understand. anyone can understand how to use the .ajax call via docs but it's the implementation of the ajax method that explains callbacks fully so that example blows. – PositiveGuy Jun 08 '15 at 04:47

1 Answers1

3

Not sure what your database object looks like but you probably want something like this, with the database results passed into the callback.

connection.invokeQuery(sqlStatement, function(rows) {
    res.render('index', { title: 'My App', customers: rows});
});
Rob
  • 12,659
  • 4
  • 39
  • 56
  • 1
    +1. I'm not finding any documentation, but based on analogous things I see on Google, I think it probably actually needs to be `function (err, rows)` rather than just `function (rows)`; but either way, this is definitely the right idea. – ruakh Jun 08 '15 at 03:36
  • 1
    yeah, most database adapters take err as the first argument but he seems to be wrapping the adapter in a custom object so that's just my assumption. – Rob Jun 08 '15 at 03:38
  • ah I see. So when would I be using promises or watchers then? – PositiveGuy Jun 08 '15 at 03:39
  • check out my latest update, I included my custom invokeQuery method. – PositiveGuy Jun 08 '15 at 03:40
  • @WeDoTDD: You use promises when you want the syntax to *look* different: `con.query(sql).then(function(){})` instead of `con.query(sql,function(){})`. Both still require callbacks and are semantically the same. It's just that some people think promises look "nicer" (note: I'm ignoring the differences in how some promise libraries handle errors) – slebetman Jun 08 '15 at 04:03
  • 2
    @Rob: Perhaps you should show that he needs to modify the implementation of `invokeQuery` in order to add a callback. The OP seems to think that adding a callback is simply passing a function as the second argument instead of writing a function that accepts a function as an argument. – slebetman Jun 08 '15 at 04:06
  • thanks slebetman, did not realize that – PositiveGuy Jun 08 '15 at 04:08
  • figured it out, let me post it – PositiveGuy Jun 08 '15 at 04:23