未捕获的 DOMException:无法从 'IDBRequest' 读取 'result' 属性:请求尚未完成

Uncaught DOMException: Failed to read the 'result' property from 'IDBRequest': The request has not finished

我通过调用 javascript 的另一个文件中的方法将数据绑定到 3 个下拉列表。 IndexedDB下拉加载的数据源。 数据绑定 JS,

function bindStateData(){
     getMstStates("#state");
     getMstStates("#drstate");
     getMstStates("#cstate");
   }

数据库管理器 js,

function getMstStates(state) {
    var request = indexedDB.open('AppDatabase', '3')
    request.onsuccess = function (e) {
        var dbInstance = e.target.result;
        let transaction = dbInstance.transaction("MstStates", "readonly");
        const statesDetailsStore = transaction.objectStore("MstStates");
        stateData = statesDetailsStore.getAll();
        stateData.onsuccess = function () {
            var MstStatesMap = new Object();
            var len = stateData.result.length,
            i;
            for (i = 0; i < len; i++) {
                MstStatesMap[i] = {};
                MstStatesMap[i].StateId = stateData.result[i].StateId;
                MstStatesMap[i].StateName = stateData.result[i].StateName;
            }
            callBackOptionItems(MstStatesMap, state);
        }
        stateData.onerror = function () {
            console.log('getMstStates : ' + e);
        }
     }
    request.onerror = function (e) { console.log('getMstStates : ' + e); }
}

function callBackOptionItems(MstOptionMap, objID) {
    for (var j in MstOptionMap) {
        var k, v
        var tem = 0;
        for (var i in MstOptionMap[j]) {
            if (tem == 0) {
                k = MstOptionMap[j][i]
            } else {
                v = MstOptionMap[j][i];
            }
            tem++
        }
        var el = $(objID);
        $(el).append('<option value="' + k + '">' + v + '</option>');

    }
    if (objID == '#select-history') {
        $("#select-history").append('<option value="4">Other</option>');
    }
}

当我尝试加载数据时出现以下错误,

Uncaught DOMException: Failed to read the 'result' property from 'IDBRequest': The request has not finished.
    at IDBRequest.stateData.onsuccess

同样的代码逻辑用WebSQL写的,运行正常,可惜从iOS13版本起取消了对websql的支持

我已经解决了这个问题。罪魁祸首是,

var len = stateData.result.length;

正确的代码如下,

    var _request = statesDetailsStore.getAll();
    _request.onsuccess = function (event) {
        var MstStatesMap = new Object();
        var stateData = event.target.result;
    }