0

I am trying to create a database handler class in javascript. I would like to call the class by simply using:

var databaseHandler = new DatabaseHandler();
result = databaseHandler.getResult("SELECT * FROM login");

I have created the class and used a callback for the ajax function (so as to wait for the ajax result to be returned). But all I am still receiving "undefined" as my result. If I use console.log(a) inside of the onComplete function, I get an array of the intended results.

(function(window){

    //Database class
    function DatabaseHandler(){

        //Query
        this.query = function(query, whenDone){
            request = $.ajax({
                url: "../optiMizeDashboards/php/DatabaseQuery.php",
                type: "POST",
                data: {query : query},
                dataType: "JSON"
            });

            request.done(function(output) {
                whenDone(output);
            });

            request.fail(function(jqXHR, textStatus) {
                console.log(textStatus);
            });
        };

        //Get result
        this.getResult = function(query){
            this.query(query, this.onComplete);
        };

        //Ajax callback
        this.onComplete = function(a){
            return a;
        };
    }

    //Make available to global scope
    window.DatabaseHandler = DatabaseHandler;

}(window))

My question is: Is this something to do with the variable scope, or the way that ajax works? I have read all the answers explaining that ajax is ASYNC and I thought I had handled that by using a callback function "onComplete"

Any help on this topic would be greatly appreciated!

1
  • you have not implemented the callback system correctly Commented Jul 7, 2014 at 12:14

1 Answer 1

3

You will not be able to return result immediately from calling getResult because underlying jQuery POST request is Asynchronous, instead you need to be passing a callback function which eventually will receive a result from server.

something like that:

(function(window){
    //Database class
    function DatabaseHandler(){
        //Query
        this.query = function(query, whenDone){
            request = $.ajax({
                url: "../optiMizeDashboards/php/DatabaseQuery.php",
                type: "POST",
                data: {query : query},
                dataType: "JSON"
            });
            request.done(function(output) {
                whenDone(output);
            });
            request.fail(function(jqXHR, textStatus) {
                console.log(textStatus);
            });
        };
        //Get result
        this.getResult = function(query, callback){
            this.query(query, callback);
        };
    }

    //Make available to global scope
    window.DatabaseHandler = DatabaseHandler;

}(window))

// then use it like so
var databaseHandler = new DatabaseHandler();
result = databaseHandler.getResult("SELECT * FROM login", function(data) {
    //do something with data
});

PS: exposing direct SQL access to the databse on the client is very dangerous though, and I would not recommend doing that

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.