【问题标题】:Ionic framework, the method not returning the value离子框架,方法不返回值
【发布时间】:2015-10-08 18:20:27
【问题描述】:

我正在开发基于 ionic 框架和 angular js 和 ngCordova 的移动混合应用程序。我是新手。我正在使用 SQLite 插件来操作数据库。

我有以下代码

service.js

angular.module('imrapp.services', ['imrapp.config'])
.factory('DB', function(DB_CONFIG, $cordovaSQLite) {
    var self = this;
    self.db = null;


    self.firstRunTest = function() {
        if (window.cordova) {

            self.db = cordovaSQLite.openDB({
                name: DB_CONFIG.name
            }); //device
        } else {
            self.db = window.openDatabase(DB_CONFIG.name, '1.0', 'database', -1); // browser
        }
        return $cordovaSQLite.execute(self.db, 'SELECT name FROM sqlite_master WHERE type=(?) AND name=(?)', ['table', 'sync_table'])
            .then(function(result) {
                if (result.rows.length > 0) {
                    console.log(result.rows.item(0).name);
                    return result.rows.item(0).name;
                } else {
                    return "No results found";
                }
            }, function(err) {
                alert(err);
                console.error(err);
            });
    }
    return self;
})

controllers.js

angular.module('imrapp.controllers', [])

.controller('LoginCtrl', function($scope, DB) { //AuthInterceptor,
    $scope.data = {};

    $scope.login = function() {

         var firstRun = DB.firstRunTest();
        console.warn("IS FIRST RUN 2 = " + JSON.stringify(firstRun));

    }
});

当我从控制器呼叫 DB.firstRunTest 时。预期的返回是result.rows.item(0).name 的值,但它返回对象{"$$state":{"status":0}}

查询运行得非常好,因为在 DB.firstRunTest() 方法中,
console.log(result.rows.item(0).name); 给出了预期值。

任何人都可以帮助我解决这个问题,我哪里出错了。 谢谢

【问题讨论】:

    标签: angularjs ionic-framework ngcordova


    【解决方案1】:

    问题是您正在调用异步函数,因此响应将在最后完成,即使在“return self;”之后也是如此。所以它总是和它进入函数时一样。为了处理异步调用,您必须使用 Promise。首先调用

    var firstRun = DB.firstRunTest();
    

    应该是:

    DB.firstRunTest().then(function(returned) {
        firstRun = returned;
    })
    

    另一种选择是捕获错误:

    DB.firstRunTest().then(function(returned) {
        firstRun = returned;
    }, function(error) {
    //Do what you want with the error
    })
    

    那我用一个更简单的函数来解释,你自己定义就好了:

    .factory('DB', function(DB_CONFIG, $cordovaSQLite, $q) {
                    var def = $q.defer(); //Create promise
    
                   $cordovaSQLite.execute(db, 'SELECT name FROM sqlite_master WHERE type=(?) AND name=(?)', ['table', 'sync_table'])
                            .then(function(result) {
                                def.resolve(result)/*Here you should put what you want to be in "returned" 
            I suggest to put an object. I face problems everytime I resolve a simple var.*/
                            }, function(err) {
                                alert(err);
                                console.error(err);
                                def.reject(err)//Here you should put what you want to return as "error" 
                            });
                    }
                return def.promise;/*The promise will be returned asyncronously so it wouldn't be return till all the asyncronous function will be resolve. 
        If you have 2 asyncronous functions but you use the "promise.resolve()" in the first one, promise data will be catch so the second function won't return anything, so be care of "resolve" just what you need.*/
            })
    

    如果我自己解释的不好,请询问,我会尽力解释得更好:P

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-05
      • 2019-07-29
      • 2014-06-04
      • 2023-03-09
      • 1970-01-01
      • 2011-06-25
      • 2019-07-25
      相关资源
      最近更新 更多