Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error "A mutation operation was attempted on a database that did not allow mutations." when retrieving data in indexedDB

I have this simple example code:

var request = mozIndexedDB.open('MyTestDatabase');
request.onsuccess = function(event){
  var db = event.target.result;
  var request = db.setVersion('1.0');
  request.onsuccess = function(event){
    console.log("Success version.");
    if(!db.objectStoreNames.contains('customers')){
      console.log("Creating objectStore");
      db.createObjectStore('customers', {keyPath: 'ssn'});
    }
    var transaction = db.transaction([],  IDBTransaction.READ_WRITE, 2000);
    transaction.oncomplete = function(){
      console.log("Success transaction");
      var objectStore = transaction.objectStore('customers');
    };
  };
};

I am getting this:

A mutation operation was attempted on a database that did not allow mutations." code: "6

on line

var objectStore = transaction.objectStore('customers');

Can't figure out - what do I do wrong?

like image 288
Aleksandr Motsjonov Avatar asked Sep 04 '11 17:09

Aleksandr Motsjonov


2 Answers

You can create or delete an object store only in a versionchange transaction

see: https://developer.mozilla.org/en-US/docs/IndexedDB/IDBDatabase

like image 89
cyong Avatar answered Oct 02 '22 07:10

cyong


I think I found the answer. I shouldn't access objectStore inside oncomplete. I just need to do it after making new transaction. Right way is this:

var transaction = db.transaction([],  IDBTransaction.READ_WRITE, 2000);
    transaction.oncomplete = function(){
      console.log("Success transaction");
    };
var objectStore = transaction.objectStore('customers');

Btw, this is how exactly Mozilla's MDN shows. https://developer.mozilla.org/en/IndexedDB/Using_IndexedDB#section_10

like image 31
Aleksandr Motsjonov Avatar answered Oct 02 '22 06:10

Aleksandr Motsjonov