Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'IDBObjectStore': The transaction has finished. #12

Closed
Lunatic83 opened this issue Nov 21, 2014 · 3 comments
Closed

'IDBObjectStore': The transaction has finished. #12

Lunatic83 opened this issue Nov 21, 2014 · 3 comments

Comments

@Lunatic83
Copy link

Hi bramski,
I'm opening another issue, because I tryed to develope another way to solve the issue #11, using Restangular, I would fetch my api entity if is not stored inside indexedDB store.

$indexedDB.openStore(entityName, function(table) {
var d = $q.defer();
var promise = d.promise;

table.find(id).then(function(success) { //Change if id is used with index
    return d.resolve(success);
}, function(error) {
    Restangular.one(entityName, id).get().then(function(result) {
        table.insert(result, function(result) {
            console.log('insert ok');
        });
    });
    return d.reject(error);
});

return promise;

}, 'readwrite');

I have Error: Failed to execute 'add' on 'IDBObjectStore': The transaction has finished.

I understand the reason for async callback for restangular refers to table after the transaction is closed, is there any way to keep the transaction open and manage it inside restangular callback?

Thanks a lot for your support.

G

@bramski
Copy link
Owner

bramski commented Nov 21, 2014

Hey @Lunatic83 , this is not really an issue with the library. The problem here is that you are opening an XHR within your transaction. That's not going to work given the async nature of javascript based promises. You also cannot both error and attempt to insert something in your transaction. If your transaction results in error, your transaction will be rolled back.

My recommended refactoring..(

var getItemAndAddToDb;

getItemAndAddToDb = function(entityName, id) {
  return Restangular.one(entityName, id).get().then(function(result) {
    return $indexedDB.openStore(entityName, function(store) {
      return store.insert(result);
    });
  });
};

$indexedDB.openStore(entityName, function(table) {
  return table.find(id).then(function(item) {
    return item;
  })["catch"](function(error) {
    return getItemAndAddToDb(entityName, id);
  });
});

@bramski
Copy link
Owner

bramski commented Nov 21, 2014

I'm going to close this issue though as it's not really a bug persay. It would also be excellent if you could open & place your questions into stackoverflow and tag it "angular-indexed-db". That sort of method of first asking stack overflow questions seems most helpful to others who run into the same problems. Let me know when you open a question there and I am happy to direct you to a better solution.

@bramski bramski closed this as completed Nov 21, 2014
@lucd
Copy link

lucd commented Apr 9, 2015

Huhz

On Friday, November 21, 2014, Bram Whillock notifications@github.com
wrote:

I'm going to close this issue though as it's not really a bug persay. It
would also be excellent if you could open & place your questions into
stackoverflow and tag it "angular-indexed-db". That sort of method of first
asking stack overflow questions seems most helpful to others who run into
the same problems. Let me know when you open a question there and I am
happy to direct you to a better solution.


Reply to this email directly or view it on GitHub
#12 (comment)
.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants