承诺在Ionic应用程序中未在设备上返回数据

时间:2015-11-30 16:03:02

标签: angularjs cordova ionic-framework ionic pouchdb

关于这个bug,我上周一直在拔头发,所以我真的希望有人能告诉我哪里出错了。

我有一个离子应用程序,我正在使用PouchDB和SQLlite cordova插件。当应用程序加载时,如果本地存储中存在任何现有数据,我会抓住它并将其添加到PouchDB。这在桌面浏览器(Chrome和Safari)以及iOS模拟器中都能正常工作,但不适用于设备......

这是失败的代码:

PouchdbFactory.updateCodes(oldCodesArray).then(function() { // <-- this works, it updates the pouchDB with a reformed array of the old localstorage data

        PouchdbFactory.getAllCodes().then(function(codes) { // Codes is always empty when on the device, but contains all the data in pouchDB when ran in the ios simulator or on desktop chrome/safari
            $scope.codes = codes;
            $rootScope.$emit('codesUpdated', codes);
            $localstorage.removeObject('codes');
        }).catch(function(error) {
            console.log(error); // I don't get any errors on device
        });
});

继承我的服务方法:

.factory('PouchdbFactory', ['$q', function ($q) {

    var db,
        codes,
        codesPromise;

    var initDB = function() {
        db = new PouchDB('codes', {adapter: 'websql', auto_compaction: true}); //auto_compaction: true
    }

    var deleteDB = function() {
        db.destroy().then(function() { console.log('database destroyed') });
    }

    var addCode = function(code) {
        return $q.when(db.post(code));
    }

    var updateCode = function(code) {
        return $q.when(db.put(code));
    }

    var updateCodes = function(codes) {

        var postPromises = [];

        codes.forEach(function(code) {
            console.log('looping in new updateCodes');
            postPromises.push(addCode(code));
        });

        return $q.all(postPromises);
    }

    var deleteCode = function(code) {
        return $q.when(db.remove(code));
    }

    var findIndex = function(array, id) {
        var low = 0, high = array.length, mid;
        while (low < high) {
            mid = (low + high) >>> 1;
            array[mid]._id < id ? low = mid + 1 : high = mid
        }
        return low;
    }

var onDatabaseChange = function(change) {

    console.log('db.changes() has detected a change');

    var index = findIndex(codes, change.id);
    var code = codes[index];

    if (change.deleted) {
        if (code) {
            codes.splice(index, 1); // delete
        }
    } else {
        if (code && code._id === change.id) {
            codes[index] = change.doc; // update
        } else {
            codes.splice(index, 0, change.doc) // insert
        }
    }
}

var getAllCodes = function() {
    if (!codesPromise) {
        codesPromise = $q.when(db.allDocs({ include_docs: true})).then(function(docs) {

            // Each row has a .doc object and we just want to send an
            // array of code objects back to the calling controller,
            // so map the array to contain just the .doc objects.
            codes = docs.rows.map(function(row) {
                return row.doc;
            });

            // Listen for changes on the database.
            db.changes({ live: true, since: 'now', include_docs: true}).on('change', onDatabaseChange);

            //Essentially, the call which sets up the changes listener is async. Depending on timing, this means that the documents inserted during the bulk_docs call might be saved before the listener starts.

            return codes;
        });

        return codesPromise;
    } else {

        // Return cached data as a promise
        //return $q.when(codesPromise);

        return codesPromise;
    }
}
return {
        initDB: initDB,
        deleteDB: deleteDB,
        addCode: addCode,
        updateCode: updateCode,
        updateCodes: updateCodes,
        deleteCode: deleteCode,
        getAllCodes: getAllCodes
    }
}])

db在主离子.run()函数中初始化:

angular.module('app', ['ionic', 'app.controllers', 'ngCordova', 'app.services', 'app.filters'])

.run(function($ionicPlatform, $localstorage, $rootScope, $interval, $timeout, OtpFactory, $state, $cordovaGoogleAnalytics, $cordovaSplashscreen, $timeout, PouchdbFactory) {
    $ionicPlatform.ready(function() {

        ... other code ....

        PouchDB.debug.enable('*');
        PouchdbFactory.initDB();
    });
})

如果你错过了代码中的注释,getAllCodes()应该返回PouchDB中的所有数据(它在ios模拟器和桌面chrome ad safari上执行)但是,在设备上运行时代码始终为空...

起初我认为这是一个pouchDB问题,但这只在部署在设备上时才会发生(数据被添加到PouchDB中,但getAllCodes()返回的代码总是为空......

请有人帮忙,阻止我摆脱绞索......

提前致谢!

0 个答案:

没有答案