我有一个服务函数(使用类型记录4.0.2+webpack 4.44.1+dexie 3.0.2),它将一个jsonfile同步到Dexie数据库。但是,一旦启动数据库事务,就会得到一个错误:
Unhandled rejection: TransactionInactiveError: Transaction has already completed or failed
./node_modules/dexie/dist/dexie.mjs/Transaction.prototype._promise@http://localhost:9000/dist/bundle.js:4876:30
./node_modules/dexie/dist/dexie.mjs/Dexie.prototype._transaction@http://localhost:9000/dist/bundle.js:6488:35
./node_modules/dexie/dist/dexie.mjs/Dexie.prototype.transaction@http://localhost:9000/dist/bundle.js:6438:34
updateEDSMSector/<@http://localhost:9000/dist/bundle.js:169098:26
...
...错误出现在console.log('extracted json', json);语句之后(但下一个console.log从未发生)。我完全不知道我在这里做错了什么。我希望任何人都能给一个指针如何调试这个问题。
static async updateEDSMSector(sector_number: number[], db: EDSMLayerDB) {
const sector = await db.sectors.where('sector_number').equals(sector_number).first();
console.log("Checking for EDSM updates for sector", sector);
const updatedDate = sector.updatedDate;
const [sx, sy, sz] = sector.sector_number;
const jsonFilename = `edsm/${sx}/${sy}/${sx}_${sy}_${sz}.json.gz`;
let res = await fetch(jsonFilename, {
cache: 'no-cache',
method: 'HEAD',
});
const lastModified = new Date(res.headers.get('Last-Modified'));
console.log('updatedDate', updatedDate, 'lastModified', lastModified);
if (!updatedDate || new Date(updatedDate) < lastModified) {
res = await fetch(jsonFilename, {
cache: 'no-cache',
method: 'GET',
});
console.log('importing data');
const json = JSON.parse(ungzip(new Uint8Array(await res.arrayBuffer()),
{to: 'string'}));
console.log('extracted json', json);
await db.transaction('rw', db.sectors, db.systems, async () => {
console.log('started transaction');
await db.systems.where('sector').equals(sector.sector_number).delete();
console.log('deleted old data');
for (const system of json) {
const {coords, ...rest} = system;
const val = {...rest, ...coords};
await db.systems.add(val);
};
console.log('Added systems');
sector.updatedDate = new Date();
await db.sectors.put(sector);
});
console.log('Finished adding system data');
} else {
console.log('Systems for sector', sector, 'already up-to-date.');
}
}发布于 2020-09-22 08:40:22
问题在于,我是以异步方式运行代码的
db.mytable.where(...).each((row) => { updateEDSMSector(row.sector_number, db) };这会触发一个查询,但对于每个查询结果,都会在updateEDSMSector内部启动一个新事务。这些异步运行导致事务在旧事务尚未正确关闭时启动。我首先获取db.mytable.where().toArray(),然后在those.This上循环仍然允许异步运行我的updateEDSMSector调用,但不与关闭的事务发生冲突。
发布于 2020-09-17 12:49:04
看看您的代码片段,它似乎是一个节点程序?Dexie主要用于浏览器,但可以在节点中使用IndexedDBShim执行。是这种情况吗?然后,您必须避免异步/等待关键字,因为IndexedDBShim会过早地启动它的事务。
您可以通过将代码转到ES5来解决这一问题,或者可以避免使用异步/等待。
https://stackoverflow.com/questions/63936363
复制相似问题