Skip to content Skip to sidebar Skip to footer

How To Return Results Of Node's Sqlite3 In A Function?

I'm trying to use sqlite3 in an expressjs app (nodejs) I want to create a function that returns all the results from a select statement. This function will be called by a route tha

Solution 1:

The line "return all" in your example will be executed BEFORE this.db.all() calls your callback. In order for your code to work you need to do something like this:

var queryGetAll = 'SELECT id, title, description, modified, lat, lng, zoom FROM maps';
function Manager(){
        this.db = null;
        // Allow a callback function to be passed to getAll
        this.getAll = function(callback){
            this.db.all(queryGetAll, function(err, rows){
                if (err){
                    // call your callback with the error
                    callback(err);
                    return;
                }
                // call your callback with the data
                callback(null, rows);
                return;
            });
        }
}

Post a Comment for "How To Return Results Of Node's Sqlite3 In A Function?"