Home > Software engineering >  IndexedDB: `DOMException` while opening an `IDBObjectStore`, that the transaction has finished
IndexedDB: `DOMException` while opening an `IDBObjectStore`, that the transaction has finished

Time:12-31

When trying to open IDBObjectStore(ObjectStore in dart):

Error message

(exact copy-paste)

[ 5324 ms] DOMException: Failed to execute 'objectStore' on 'IDBTransaction': The transaction has finished.
                    dart-sdk/lib/indexed_db/dart2js/indexed_db_dart2js.dart 1233:22                   objectStore]
                    packages/scholar/scholar/client/flutter/dart/src/main.dart 21:35                  <fn>
                    dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 334:14  _checkAndCall
                    dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 339:39  dcall
                    dart-sdk/lib/html/dart2js/html_dart2js.dart 37317:58                              <fn>

Error causing function

Future<db.ObjectStore> completeTransaction(
  final db.Transaction transaction,
  final String objectStoreName,
) {
  final Completer<db.ObjectStore> completer = Completer<db.ObjectStore>.sync();

  transaction.onError.listen(completer.completeError);

  transaction.onComplete.listen((final html.Event e) {
    completer.complete((e.target as db.Transaction).objectStore(objectStoreName));
    //completer.complete(transaction.objectStore(objectStoreName)); /// also tried this
  });

  return completer.future;
}

Accessed through

void main() async {
  ...

    await completeTransaction(
      database.transactionStore(objectStoreName, "readwrite" /** also tried "readonly" */),
      objectStoreName,
    );

  ...
}

Source of the problematic function

It was forked from a core library, function, which handles db.Request(not db.Transaction) types,

Future<T> _completeRequest<T>(db.Request request) {
  var completer = new Completer<T>.sync();
  
  request.onSuccess.listen((e) {
    T result = request.result;
    completer.complete(result);
  });
  
  request.onError.listen(completer.completeError);
  
  return completer.future;
}

Thanking you,

CodePudding user response:

This happends, when the value of Database, ObjectStore, or Transaction, is stored in some variable, and used later, instead of getting a fresh value, using library functions,

hence, the result or value of the above mentioned types, should not be stored(or cached), in variables, but need to be recalled/opened each time, they are used

  • Related