服务函数返回undefined

时间:2016-01-24 00:14:13

标签: javascript angularjs cordova ionic-framework angular-services

我有服务:

  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;
    });

在app.js文件中,在离子设备就绪时,我尝试使用我的服务:

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
  });
});

所有在真实设备(iOS)上测试过,我不明白为什么这不起作用,请帮帮我,哪里出错了?

更新

就像@charlietfl说的那样,我需要为这个功能创建一个回调,但是我不知道我该怎么做

2 个答案:

答案 0 :(得分:0)

因为this.test = this.GetCachedLanguage;是在实际定义this.GetCachedLanguage之前设置的,因此设置this.GetCachedLanguage的时间是undefined

所以在this.test = this.GetCachedLanguage;之前移动return this;

答案 1 :(得分:0)

好的,我解决了这个问题。所以我们需要为查询创建回调。我找到了这个答案here

感谢Miguel Alves的出色工作!

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";
                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 );
        }
}