2

When upgading the db version in indexed db make error . "A request was aborted, for example through a call to IDBTransaction.abort."

ConstraintError: A mutation operation in the transaction failed because a constraint was not satisfied. For example, an object such as an object store or index already exists and a new one was being attempted to be created.

Anybody can help me? Thanks in advance.

var DB_NAME = 'testdb';
var DB_VER = 2;
var db = null;

function upgradeDB(event) {
    console.log('DB upgrade needed');

    var db = event.target.result;
    var txn = event.target.transaction;
    txn.oncomplete = function(evt) {
        console.log('Upgrade complete');
    };
    txn.onerror = function(evt) {
        console.log('Upgrade failed', evt);
    };

    console.log('Creating tables');
    ..........................
    ...........................
}
var request = indexedDB.open(DB_NAME, DB_VER);
request.onsuccess = function(evt) {
    db = request.result;
    console.log('test DB opened');
    callback();
};
request.onerror = function(evt) {
    console.log('DB open failed', evt);
};
request.onupgradeneeded = upgradeDB;
anfas
  • 356
  • 2
  • 3
  • 13
  • Could happen if you have another window open to the same site. Otherwise, the error is probably in the lines you left out, the ones that do the actual transaction. Also, check that you aren't hitting the storage limits: http://stackoverflow.com/questions/26483455/indexeddb-unknownerror-a-request-was-aborted-for-example-through-a-call-to-idb?rq=1. – gpgekko Dec 22 '14 at 14:47

1 Answers1

3

If you look through the IndexedDB spec (http://www.w3.org/TR/IndexedDB/) the causes of ContraintError are:

  • Creating an object store with a name already used in that database
  • Creating an index with a name already used in that object store
  • Creating a new index with unique:true constraint violated by existing data in the object store
  • Hitting the key generator limit
  • Doing an add() where the key is already present in a store
  • Doing an add() or put() where there is an index with a unique:true constraint that the generated index keys violate

So you're hitting one of those cases.

The first two createObjectStore/createIndex cases would throw an exception synchronously; an uncaught exception thrown in an "upgradeneeded" handler will abort the transaction. You could wrap your 'Creating tables' logic in a try/catch block to see if that's what's happening. (You don't share the code, so I can't point any logic flaws there.)

Creating an index with unique:true that fails due to existing data will fail asynchronously, not in response to any request, so the only sign would be the transaction abort.

The other cases would occur asynchronously in response to existing data and fail particular requests; you could have onerror handlers to detect those.

Joshua Bell
  • 7,727
  • 27
  • 30