gpt4 book ai didi

javascript - 未捕获的 InvalidStateError : Failed to execute 'transaction' on 'IDBDatabase' : A version change transaction is running

转载 作者:数据小太阳 更新时间:2023-10-29 05:02:16 25 4
gpt4 key购买 nike

我必须承认我是 indexedDB 的新手

我写了一段indexedDB的简单代码,如下:

function go() {
var req = window.indexedDB.open("Uploader", 1),
db;
req.onerror = function (e) {
console.log("Error");
};
req.onsuccess = function (e) {
db = e.target.result;
};
req.onupgradeneeded = function (e) {
console.log(db);
db = e.target.result;
var os = db.createObjectStore("Files", { keyPath: "files" });
os.createIndex("text", "text_file", { unique: false });
var trans = db.transaction(["text"], "readwrite");
var objectstore = trans.objectStore("text");
var addreq = objectstore.add("Instructions.js");
addreq.onsuccess = function (e) {
console.log("Success!");
console.dir(e);
};
};
}

它给我的错误是 Uncaught InvalidStateError: Failed to execute 'transaction on 'IDBDatabase': A version change transaction is running.

它说 版本更改事务正在运行 但据我研究,版本更改事务是通过 IDBFactory.open 方法进行的,我还没有t used 并且我指定了这个事务是readwrite 并且这个事务是在onupgradeneeded 那么为什么会出现错误呢?

我必须承认我是 indexedDB 的新手

最佳答案

versionchange 事务还允许您读写。您只需访问在 onupgradeneeded 函数中为您创建的交易。

function go() {
var req = indexeddb.open(...);
req.onupgradeneeded = function(event) {

// note that event.target === req === this, use whatever you like
var db = event.target.result;

// createObjectScore implicitly uses the versionchange txn running
// here, without telling you, basically a convenience function
var objectStore = db.createObjectStore(...);

// the important part that i am describing in this answer,
// grab the handle of the versionchange txn
// that is already running and was created for you
// note again that event.target === req, you could just do
// req.transaction or this.transaction here.
// note this is a property of the open request, not a method. do NOT
// confuse this with the method transaction() that is used to create a
// new transaction.
var txn = event.target.transaction;

// note that txn.objectStore(...) will work here, because the
// createObjectStore call earlier guarantees the store exists here
// within the implicit upgrade txn
var addRequest = txn.objectStore(...).add('value');

// side note: if in previous line we did:
// var objectStoreRetrievedFromTxn = txn.objectStore(...);
// then that variable is equal to the same variable returned from
// db.createObjectStore earlier in this function. Both are simply handles
// (references, pointers, whatever you want to call it) to the store.

// kind of dumb, but you could do this just to log something
addRequest.onsuccess = function() {console.log('Success!');};
};

// called once upgrade txn completes (if it even needed to run),
// and db is opened
req.onsuccess = function(event) {
console.log('upgrade txn completed and db is now connected');
// here, create whatever readwrite or readonly txns you want, note these
// txns are separate from and different than the versionchange txn from
// before, because that is a unique transaction only available within
// onupgradeneeded. however, both versionchange and readwrite are similar
// in that both support calls to put or add
};
}

您遇到错误是因为您在版本更改事务仍在运行时尝试启动第二个事务。

关于javascript - 未捕获的 InvalidStateError : Failed to execute 'transaction' on 'IDBDatabase' : A version change transaction is running,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33709976/

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