Javascript nested functions not returning

Posted by Achintha Samindika on Stack Overflow See other posts from Stack Overflow or by Achintha Samindika
Published on 2013-06-25T16:18:54Z Indexed on 2013/06/25 16:21 UTC
Read the original article Hit count: 197

Filed under:

I'm building a phonehap application. I'm using an web sql and everything works fine till data retival.

function getItemGroups(){

    var items_groups = new Array();
    var db = window.openDatabase("merbokDB", "1.0", "MerbokDB", 5232394);

    db.transaction(
        function(tx){
            tx.executeSql('SELECT * FROM item_groups',[],
                function(tx,result){
                    if(result.rows.length > 0){

                        var len = result.rows.length;

                        for (var i=0; i<len; i++){

                            items_groups.push(result.rows.item(i).item_group);

                        }
                        console.log(items_groups.join());

                    }
                }
                ,errorCB);
        },
        errorCB);

    return items_groups;
}
var myproducts = getItemGroups();

My problem was when I run the code "myproducts" variable is blank. but the I can see

console.log(items_groups.join());

following line printing the values in console. Is I'm wrong in the way I returning?

© Stack Overflow or respective owner

Related posts about JavaScript