gpt4 book ai didi

javascript - 未捕获的 InvalidStateError : Failed to read the 'result' property from 'IDBRequest' : The request has not finished

转载 作者:数据小太阳 更新时间:2023-10-29 06:10:18 31 4
gpt4 key购买 nike

我需要你们的帮助。

我正在使用 indexedDB。我需要使用 Javascript 从数据库中的表中读取记录,但我收到一条错误消息,指出来自 Chrome 浏览器 V52 Uncaught InvalidStateError: Failed to read the 'result' property from 'IDBRequest ': 请求未完成。

下面是我的Javascript代码

变量数据库; var availableJobs = 0;

    window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;

var request = window.indexedDB.open("MyDbName", 1);

request.onsuccess = function (event) {
db = request.result;
}

request.onblocked = function (event) {
db = "blocked...";
};

request.onerror = function (event) {
db = "Error...";

};

var objectStore = db.transaction("ActionCard").objectStore("ActionCard");

objectStore.openCursor().onsuccess = function (event) {
var cursor = event.target.result;

if (cursor) {
if (cursor.value.ActionCardStatusId == 1 || cursor.value.ActionCardStatusId == 3) {
availableJobs++;
}

cursor.continue();
}

$("#availableJobs").html(availableJobs);
}

我在这一行收到一条错误消息

var objectStore = db.transaction("ActionCard").objectStore("ActionCard");

最佳答案

您需要了解如何编写异步 Javascript。您的 db 变量在您访问它时未定义。

不要这样做:

var r = indexedDB.open();
var db = null;
r.onsuccess = function(event) { db = event.target.result); }

这样做:

var r = indexedDB.open();
r.onsuccess = function(event) {
var db = event.target.result;
};

是的,这意味着 db 在 onsuccess 函数的范围之外不可用。停止尝试在其范围之外使用它,否则您将遇到您遇到的问题。

关于javascript - 未捕获的 InvalidStateError : Failed to read the 'result' property from 'IDBRequest' : The request has not finished,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38973744/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com