未捕获的 DOMException:无法在"IDBObjectStore"上执行'delete':事务未激活



当我试图将tensorflowjs模型保存在IndexedDb中时,遇到了这个错误。基本上,我正在开发一款离线网络应用程序,它可以实时预测情绪。为了让我的应用程序更快,我必须使用索引数据库。前端在Reactjs中。错误出现在以下行:

await this.model.save('indexeddb://' + INDEXEDDB_KEY);

这是代码。

try {
this.model = await tf.loadLayersModel('indexeddb://' + INDEXEDDB_KEY);
// Safe to assume tensorflowjs database and related object store exists.
// Get the date when the model was saved.
try {
const db = await openDB(INDEXEDDB_DB, 1, );
const item = await db.transaction(INDEXEDDB_STORE)
.objectStore(INDEXEDDB_STORE)
.get(INDEXEDDB_KEY);
const dateSaved = new Date(item.modelArtifactsInfo.dateSaved);
await this.getModelInfo();
console.log(this.modelLastUpdated);
if (!this.modelLastUpdated  || dateSaved >= new Date(this.modelLastUpdated).getTime()) {
console.log('Using saved model');
}
else {
this.setState({
modelUpdateAvailable: true,
showModelUpdateAlert: true,
});
}
}
catch (error) {
console.warn(error);
console.warn('Could not retrieve when model was saved.');
}
}
// If error here, assume that the object store doesn't exist and the model currently isn't
// saved in IndexedDB.
catch (error) {
console.log('Not found in IndexedDB. Loading and saving...');
console.log(error);
this.model = await tf.loadLayersModel(MODEL_PATH);
console.log(this.model,"model")
await this.model.save('indexeddb://' + INDEXEDDB_KEY);
}
}
// If no IndexedDB, then just download like normal.
else {
console.warn('IndexedDB not supported.');
this.model = await tf.loadLayersModel(MODEL_PATH);
}
this.setState({ modelLoaded: true });

// Warm up model.
// let prediction = tf.tidy(() => this.model.predict(tf.zeros([1, IMAGE_SIZE, IMAGE_SIZE, 3])));
// prediction.dispose();

}

''

如果没有挂起的请求,事务将关闭。看起来你的等待正在做一些非indexeddb的事情,所以很自然,因为你没有立即对事务上的一些请求进行排队,所以它会关闭,然后当你最终对事务进行请求时,它会在那时关闭(不活动(。

在完成其他异步操作后启动事务。在启动事务和向事务添加请求之间不要做异步操作。

最新更新