IndexedDb 的延迟 then of then 是未定义的

Deferred then of then is undefined for IndexedDb

我正在使用 CanJs 并且正在学习 jquery deferred 但我遇到了问题。 我创建了一个控制器作为 Singleton 来管理 IndexedDb 中的数据。

首先,我创建了一个像这样的 openDb 函数:

openDbDeferred: null,

    openDb: function (dbName, dbVersion) {
        console.log('Open DB...');

        var openDbDeferred = this.openDbDeferred;

        if (!openDbDeferred || openDbDeferred.isRejected()) {
            openDbDeferred = $.Deferred();

            var db;
            var req = indexedDB.open(dbName, dbVersion);

            req.onsuccess = function (evt) {
                db = this.result;
                console.log('openDB SUCCESS');
                openDbDeferred.resolve(db);
            };

            req.onerror = function (evt) {
                console.error("[ERROR] openDb: " + evt);
                openDbDeferred.reject();
            };

            req.onupgradeneeded = function (evt) {
                console.log('openDb.onupgradeneeded');
                var db = evt.target.result;
                var store = db.createObjectStore('sessioni', {keyPath: 'idSession'});

                store.createIndex('by_url', 'url', {unique: false});
                store.createIndex('by_startDate', 'startDate', {unique: false});
                store.createIndex('by_endDate', 'endDate', {unique: false});
            };

        }
        return openDbDeferred.promise();
    }

然后我创建了一个函数来检索数据库中的所有数据:

getFilesList: function () {
        var getDataDeferred;
        return this.openDb('session-db', 1).then(function (db) {
            console.log('Find all records...');

            getDataDeferred = $.Deferred();
            var tx = db.transaction("sessioni", "readwrite");
            var store = tx.objectStore("sessioni");
            var items = [];

            tx.oncomplete = function() {
                //console.log(items);
                getDataDeferred.resolve(items);
                console.log('Transazione getFilesList completata');
            };

            tx.onfailure = function(evt) {
                getDataDeferred.reject();
                console.error('[ERROR] Transazione getFilesList fallita: ' + evt);
            };

            var cursorRequest = store.openCursor();

            cursorRequest.onsuccess = function (evt) {
                var cursor = evt.target.result;
                if (cursor) {
                    items.push(cursor.value);
                    cursor.continue();
                }
            };
            cursorRequest.onerror = function (error) {
                console.error('findAll [ERROR]: ' + error);
            };
        });

        return getDataDeferred.promise();
    }

我在另一个控制器中声明了这个控制器来调用 getFilesList 函数:

retreiveAllData: function() {
        return this.sessionManageModel.getFilesList().than(function(items) {
            console.log(items)
            return items;
        });
    }

当 retreiveAllData 函数被调用时,它 returns 'undefined' 因为 items 是 'undefined'.

如何在 retreiveAllData 函数中获取项目?

您的 getFilesList 函数中有两个 return 语句。第二个实际上应该在 then 回调内 - 如您所见,目前 returns undefined

getFilesList: function () {
    // no need to declare deferred variable outside of the callback
    return this.openDb('session-db', 1).then(function (db) {
        var getDataDeferred = $.Deferred();
        … // do all the stuff
        return getDataDeferred; // place the `return` here
    }); 
    // not here!
}