gpt4 book ai didi

javascript - 无法在 'createObjectStore' 上执行 'IDBDatabase'

转载 作者:可可西里 更新时间:2023-11-01 01:58:47 28 4
gpt4 key购买 nike

为什么会出现错误?

我的代码:

var idb = window.indexedDB ||      // Use the standard DB API
window.mozIndexedDB || // Or Firefox's early version of it
window.webkitIndexedDB; // Or Chrome's early version

var IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction;
var IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange;

var dbName='nameDb';

var idbRequest=idb.open(dbName,'4.67' /*,dbDescription */);

idbRequest.onsuccess=function (e) {
debugger

var db=e.target.result;

if (!db.objectStoreNames.contains('chat')){
co=db.createObjectStore('chat',{'id':100});
};

if (!db.objectStoreNames.contains('iam')){
co1=db.createObjectStore('iam');
};
};

idbRequest.onerror = function (e) {
debugger
};

Uncaught InvalidStateError: Failed to execute 'createObjectStore' on 'IDBDatabase': The database is not running a version change transaction. index.html:37 idbRequest.onsuccess

最佳答案

您不能在 onsuccess 方法中创建 objectStore。您只能在 upgradeneeded 事件中执行此操作。

引用自文档:

When you create a new database or increase the version number of an existing database (by specifying a higher version number than you did previously, when Opening a database), the onupgradeneeded event will be triggered. In the handler for this event, you should create the object stores needed for this version of the database

参见 documentation .

关于javascript - 无法在 'createObjectStore' 上执行 'IDBDatabase',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24382085/

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