javascript 错误“在不允许突变的数据库上尝试了突变操作。” 在 indexedDB 中检索数据时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7301064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error "A mutation operation was attempted on a database that did not allow mutations." when retrieving data in indexedDB
提问by Aleksandr Motsjonov
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
试图在不允许突变的数据库上进行突变操作。”代码:“6
on line
在线的
var objectStore = transaction.objectStore('customers');
Can't figure out - what do I do wrong?
想不通 - 我做错了什么?
采纳答案by Aleksandr Motsjonov
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:
我想我找到了答案。我不应该在 oncomplete 中访问 objectStore。我只需要在进行新交易后做。正确的做法是这样的:
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
顺便说一句,这正是 Mozilla 的 MDN 显示的方式。https://developer.mozilla.org/en/IndexedDB/Using_IndexedDB#section_10
回答by cyong
You can create or delete an object store only in a versionchange transaction
您只能在 versionchange 事务中创建或删除对象存储
see: https://developer.mozilla.org/en-US/docs/IndexedDB/IDBDatabase
请参阅:https: //developer.mozilla.org/en-US/docs/IndexedDB/IDBDatabase
回答by Wladimir Palant
I didn't try that code but judging by the documentationyou 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.
我没有尝试该代码,但根据文档判断,您不应该将空列表作为第一个参数传递给db.transaction()
- 应该是db.transaction(["customers"], ...)
因为您想使用该对象存储。