javascript - 在 indexedDB 中检索数据时出现错误 "A mutation operation was attempted on a database that did not allow mutations."

标签 javascript indexeddb

我有这个简单的示例代码:

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');
    };
  };
};

我得到这个:

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

在线

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

无法弄清楚 - 我做错了什么?

最佳答案

您只能在版本更改事务中创建或删除对象存储

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

关于javascript - 在 indexedDB 中检索数据时出现错误 "A mutation operation was attempted on a database that did not allow mutations.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7301064/

相关文章:

javascript - 未捕获的TypeError : Cannot read property 'transaction' of null with an indexeddb

javascript - var 关键字和没有 var 的区别

javascript - 我怎么知道 indexedDB 请求循环是否已经结束?

javascript - 激活 serviceworker 时使用 fetch 创建 indexeddb

javascript - javascript如何存储数组

javascript - 类型错误 : Illegal invocation on function alias

javascript - Webpack 插件 API : getting source maps for a module during parsing

javascript - 抓取动态表单 WWW::Mechanize Perl

javascript - 如何搜索项目、返回其 ID 并更新 Azure 移动服务中的记录

javascript - ui路由器查询参数