bramski / angular-indexedDB

An angularjs serviceprovider to utilize indexedDB with angular
165 stars 49 forks source link

Failed to execute 'index' on 'IDBObjectStore': The specified index was not found. #30

Open neslinesli93 opened 9 years ago

neslinesli93 commented 9 years ago

In my code I have a function like that:

findOneByIndex: function(entityName, id, index) {
    var d = $q.defer();
    var promise = d.promise;

    $indexedDB.openStore(entityName, function(table) {
        table.findWhere(table.query().$index(index).$eq(id)).then(function(result) {
            if (result.length === 0) {
                console.log('No objects found');
                return d.reject(result);
            } else {
                return d.resolve(result[0]);
            }
        }, function(error) {
            console.log(error);
        });
    }, 'readwrite').then(null, function(error) {
        console.log(error);
    });

    return promise;
}

If I try to query IDB with an index that does not exist, I would expect the exception gets caught and logged. The problem is when I try to test this behaviour, I get:

  Uncaught NotFoundError: Failed to execute 'index' on 'IDBObjectStore': The specified index was not found.
  at angular-mocks/angular-mocks.js:9

I was wondering if it's a problem with angular mocks or with angular-indexedDB. In my test I am injecting $indexedDB

bramski commented 9 years ago

I'm on extended vacation right now and may get back to looking at this in a week. You lack .catch on your useage of promises here.

The "uncaught" is from an exception being thrown in promise handling and there being no catch in place to handle it.

I'm fairly certain the error is in your code though and I'm not able to help you out. If you do find a solution and it turns out to be a problem with the lib, make a repro and submit a PR if you can.

Best of luck. On May 5, 2015 7:07 AM, "Tom Pom" notifications@github.com wrote:

In my code I have a function like that:

findOneByIndex: function(entityName, id, index) { var d = $q.defer(); var promise = d.promise;

$indexedDB.openStore(entityName, function(table) {
    table.findWhere(table.query().$index(index).$eq(id)).then(function(result) {
        if (result.length === 0) {
            console.log('No objects found');
            return d.reject(result);
        } else {
            return d.resolve(result[0]);
        }
    }, function(error) {
        console.log(error);
    });
}, 'readwrite').then(null, function(error) {
    console.log(error);
});

return promise;

}

If I try to query IDB with an index that does not exist, I would expect the exception gets caught and logged. The problem is when I try to test this behaviour, I get:

Uncaught NotFoundError: Failed to execute 'index' on 'IDBObjectStore': The specified index was not found. at angular-mocks/angular-mocks.js:9

I was wondering if it's a problem with angular mocks or with angular-indexedDB. In my test I am injecting $indexedDB

— Reply to this email directly or view it on GitHub https://github.com/bramski/angular-indexedDB/issues/30.

neslinesli93 commented 9 years ago

Could you be more precise on where .catch is missing? Because from the Angular documentation

promise.catch(errorCallback)

is a shortand for

promise.then(null, errorCallback)

I tried to use the function findBy(index, id) instead of findWhere(), but the result is the same. My point is: shouldn't the error be handled inside the library, in the form of a promise being rejected? For example, if you try to call openStore() on a non existing database, the promise is rejected and the error is handled correctly as well. Looking at angular-indexedDB code, all the functions that take an index as a parameter do not check the integrity of it and they concatenate other functions call assuming the index is correct.

neslinesli93 commented 9 years ago

Okay I've looked at the code a bit more in depth and I can try to submit a pr in the next days. It's just about checking if the index provided by the user is inside IDBObjectStore.indexNames, and it addresses the functions findBy() and eachWhere().

bramski commented 9 years ago

Feel free.

neslinesli93 commented 9 years ago

Oh well, I tried and failed. The core problem was that i was trying to put two transactions inside one function, and it was always complaining about "$digest being already in progress" or something like that