0

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

我有以下代码

服务.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;
})

控制器.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);给出了预期的值。

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

4

1 回答 1

0

问题是您正在调用异步函数,因此响应将在最后完成,即使在“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

于 2015-10-08T10:20:27.183 回答