最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Error "A mutation operation was attempted on a database that did not allow mutations." when retri

programmeradmin2浏览0评论

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?

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?

Share Improve this question edited May 25, 2014 at 5:37 Josh 18.7k7 gold badges54 silver badges72 bronze badges asked Sep 4, 2011 at 17:50 Aleksandr MotsjonovAleksandr Motsjonov 1,3283 gold badges14 silver badges25 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 7

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

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

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

I didn't try that code but judging by the documentation you shouldn't pass an empty list as first parameter to db.transaction() - it should rather be db.transaction(["customers"], ...) because you want to work with that object store.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论