Ad

Service Function Return Undefined

I have service:

  var CacheService = angular.module('test.cacheService', []);

    CacheService.service('CacheService', function($cordovaSQLite){
      var db = null;

      this.InitDB = function(){
        db = $cordovaSQLite.openDB( 'my.db', 1 );
        $cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXISTS settings (key TEXT, value TEXT)");
        $cordovaSQLite.execute(db, "INSERT INTO settings (key, value) VALUES(?, ?)" , ['language', 'en']);
      };

      this.GetCachedLanguage = function(){
        $cordovaSQLite.execute(db, 'SELECT * FROM settings WHERE key = "language"').then(function(result){
// OUTPUT DEFINED        
alert(result.rows.item(0)['value'] + ' ' + typeof result.rows.item(0)['value']); 
                return result.rows.item(0)['value'];
            });
      };
      return this;
    });

In app.js file, on ionic device ready i try to use my service:

var app = angular.module('test', ['ionic','ngCordova', 'test.cacheService'])
app.run(function($ionicPlatform, CacheService) {
  $ionicPlatform.ready(function() {
    if(window.StatusBar) {
      StatusBar.styleDefault();
    }

    CacheService.InitDB();
    alert(CacheService.GetCachedLanguage()); //alert show undefined
  });
});

All tested on real device (iOS) and i don't understand why this not works, please help me, where i take a mistake?

UPDATE:

Like @charlietfl says, i need to create a callback, for this function, but i don't understand how can i do this

Ad

Answer

Okay, i fix this problem. So we need to create callback for our queries. I find this answer here

Thanks to Miguel Alves for the great work!

database("SELECT * FROM USER", function(result){
    console.log(result);
});

//FUNCTION
function database(sql, callback) {

    if(!callback) { callback = function(r) { console.log(r); } }

    var database_name = "Database",
        database_version = "1.0",
        database_displayname = "DatabaseName",
        database_size = 1000000,
        result = {
            error: -1,
            message: "",
            len: 0,
            rows: {}
        },
        db = window.openDatabase(database_name, database_version, database_displayname, database_size);
        db.transaction(
                        function (tx) {
                            tx.executeSql(sql, [], querySuccess, errorCB);
                        }, function (tx, results) {
                            //SUCCESS
                        }, function (err) {
                            //ERROR
                        }
        );
        function querySuccess(tx, results) {
            if (results) {
                result.len = results.rows.length;
                result.message = "Success";
        );
        function querySuccess(tx, results) {
            if (results) {
                result.len = results.rows.length;
                result.message = "Success";
                for (var i=0; i<result.len; i++){
                    result.rows[i] = results.rows.item(i);
                }
            } else {
                result.len = 0;   
            }
            callback( result );
        }
        function errorCB(err) {
            result.error = err.code;
            result.message = err.message;
            callback( result );
        }
}
Ad
source: stackoverflow.com
Ad