错误“在不允许突变的数据库上尝试了突变操作。”在索引eddb中检索数据时
-
22-10-2019 - |
题
我有一个简单的示例代码:
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');
};
};
};
我明白了:
在不允许突变的数据库上尝试了突变操作。“代码:” 6
在线的
var objectStore = transaction.objectStore('customers');
不知道 - 我做错了什么?
解决方案 2
我想我找到了答案。我不应该在oncomplete内访问对象存储。进行新交易后,我只需要这样做。正确的方法是:
var transaction = db.transaction([], IDBTransaction.READ_WRITE, 2000);
transaction.oncomplete = function(){
console.log("Success transaction");
};
var objectStore = transaction.objectStore('customers');
顺便说一句,这就是Mozilla的MDN显示的方式。 https://developer.mozilla.org/en/indexeddb/using_indexeddb#section_10
其他提示
您只能在版本change事务中创建或删除对象存储
看: https://developer.mozilla.org/en-us/docs/indexeddb/idbdatabase
我没有尝试该代码,但请判断 文档 您不应该将空列表作为第一个参数将 db.transaction()
- 应该是 db.transaction(["customers"], ...)
因为您想使用该对象存储。
不隶属于 StackOverflow