Ionic ngCordova SQLite 执行方法 - 在调用 'then' 之后控制应该 return

Ionic ngCordova SQLite execute method - control should return after 'then' is called

我有一个功能 'insertAllLinksFunc'-它在数据库中插入数据。 当在 'insertAllLinksFunc' 内部调用 execute 方法时,'then' 方法完成之前的控制 returns。我希望控件仅在 'then' 方法完成后才 return。

这是日志:

            8     697189   log      test : inside test              
            9     697193   log      insertAllLinksFunc : inside insertAllLinks : arr length=1               
            10    697195   log      insertAllLinksFunc : ret=               
            11    697196   log      test : arr0=                
            12    697197   log      test : exitting test                
            13    697206   log      insertAllLinksFunc : item=LinkEntity[linkId=1443150697190, title=title-1443150697190, imgPath=imgPath-1443150697190]

我想要的是日志 13 的代码应该在日志 9 和日志 10 的代码之间执行

代码:

myController.js

            var db=null;
            var myModule = angular.module('MyController', ['ionic', 'ngCordova'])
            .run(function($ionicPlatform, $cordovaSQLite) {
              $ionicPlatform.ready(function() {
                console.log('inside onReady');
                db = $cordovaSQLite.openDB("my.db");

                $cordovaSQLite.execute(db, "create table if not exists POSTS_TBL( POST_ID numeric primary key , CREATE_DT text not null, POST_TYPE text not null, TH_LOCAL_IMG_PATH text , TH_SERVER_IMG_PATH text , LINK_URL text , TITLE text , CAPTION text , MESSAGE text , DESCRIPTION text , LOCAL_IMG_PATH text , SERVER_IMG_PATH text , POST_JSON text not null)");
                console.log('exitting onReady, db='+db);
              });
            })

            .factory('queryFactory', function($cordovaSQLite){

insertAllLinksFunc = function(arr){
    console.log('insertAllLinksFunc : inside insertAllLinks : arr length='+arr.length);
    var ret = null;
    if(arr.length>0){
        var query = "INSERT INTO POSTS_TBL (POST_ID, TITLE, CAPTION, POST_JSON, DESCRIPTION, MESSAGE, SERVER_IMG_PATH, LINK_URL, CREATE_DT, POST_TYPE) VALUES (?,?,?,?,?,?,?,?,?,?)";

        var ret = [];
        for(item in arr){
            console.log('insertAllLinksFunc : item='+arr[item].toString());
            $cordovaSQLite.execute(db, query, [arr[item].linkId, arr[item].title, arr[item].caption, arr[item].linkJSON, arr[item].description, arr[item].msg, arr[item].serverPath, arr[item].url, arr[item].createDt, Settings.POST_TYPE_LINK]).then(function(res) {
            console.log("insertAllLinksFunc : insertAllLinksFunc : INSERT ID -> " + res.insertId);
            ret.push(res);
            }, function (err) {
                console.error(err);
                return -1;
            });

        }
        console.log('insertAllLinksFunc : ret=' + ret);
        return ret;
    }
}
            return {
                insertAllLinks : insertAllLinksFunc
            }})

app.js

            angular.module('starter', ['ionic', 'ngCordova', 'MyController'])
            .controller('myCtrl', function($scope, queryFactory){
              $scope.test = function(){
                console.log('test : inside test');    
                var time = new Date().getTime();
                var entity = new LinkEntity(time, 'title-'+time, 'imgPath-'+time, 'serverPath'+time, '{}', 'url', 'caption', 'description', time, 'msg');
                var arr = [entity];
                var arr0 = queryFactory.insertAllLinks(arr);
                console.log('test : arr0='+arr0);
                console.log('test : exitting test');
              }  
            });

entities.js

            function LinkEntity(linkId, title, imgPath, serverPath, linkJSON, url, caption, description, createDt, msg){
                this.linkId = linkId;
                this.title = title;
                this.imgPath = imgPath;
                this.serverPath = serverPath;
                this.linkJSON = linkJSON;
                this.url = url;
                this.caption = caption;
                this.description = description;
                this.createDt = createDt;
                this.msg = msg;
            }

            LinkEntity.prototype.toString = function(){
                return 'LinkEntity[linkId='+ this.linkId +', title='+this.title+', imgPath='+this.imgPath+']';
            }

sql 精简函数 $cordovaSQLite.execute 是一个异步函数。这意味着在调用该函数并插入数据时,该应用程序将继续运行。如果已插入数据,则调用“.then()”中的函数。所以 .then() 中的所有代码稍后都会被调用,您的应用程序已经编写了第 9 行和第 10 行。

要了解问题,您必须查看:AngularJS $q and promises

在您的情况下,您会遇到处理多个承诺的问题。为此,您可以将承诺收集在一个数组中并使用 $q.all: combining promises or Whosebug: how-to-chain-multiple-promises-within-and-after-a-for-loop。但我假设如果您了解如何使用 $q 并承诺您将解决主要问题。