- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我的代码如下(通常是众所周知的对象的命名约定):
var DBOpenRequest = window.indexedDB.open("messages", 6);
//...
DBOpenRequest.onupgradeneeded = function(event) {
console.log("Need to upgrade.");
var db = event.target.result;
console.log(db);
db.onerror = function(event) {
console.log("Error upgrading.");
};
// Create an objectStore for this database
var objectStore = db.createObjectStore("messages", { keyPath: "id", autoIncrement: true });
};
这对于版本 3 和 4 运行良好。当涉及到版本 5 时,我得到错误:
Failed to execute 'createObjectStore' on 'IDBDatabase': An object store with the specified name already exists. at IDBOpenDBRequest.DBOpenRequest.onupgradeneeded
createObjectStore
不是在新版本的空数据库上运行吗?如何修复错误?
我碰巧记录了 db
对象,详细信息如下:
我很好奇为什么摘要行中的版本号和展开时的版本号不同。
最佳答案
Isn't the createObjectStore operating on a new version of the database which is empty?
当您得到 upgradeneeded
时,数据库处于您之前保留它的任何状态。由于您不知道用户会访问您的代码的哪个版本,因此您需要查看事件的 oldVersion
以找出那是什么。典型的模式是这样的:
var rq = indexedDB.open('db', 5);
rq.onupgradeneeded = function(e) {
var db = rq.result;
if (e.oldVersion < 1) {
// do initial schema creation
db.createObjectStore('users');
}
if (e.oldVersion < 2) {
// do 1->2 upgrade
var s = db.createObjectStore('better_users');
s.createIndex('some_index', ...);
db.deleteObjectStore('users'); // migrating data would be better
}
if (e.oldVersion < 3) {
// do 2->3 upgrade
rq.transaction.objectStore('better_users').createIndex('index2', ...);
}
if (e.oldVersion < 4) {
// do 3->4 upgrade
db.createObjectStore('messages', ...);
}
if (e.oldVersion < 5) {
// do 4->5 upgrade
// ...
}
}
I am curious why the version number is different in the summary line and when expanded.
那个很微妙...我相信在记录 5
时数据库已经开始升级。但是because an exception was thrown in the upgradeneeded
handler the upgrade was aborted , 和 the version number was rolled back在记录详细信息之前到 4
。
关于javascript - 获取对象存储已经存在于 onupgradeneeded 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44002460/
我试图在我的索引数据库中插入数据,但从未调用函数 onupgradeneed,只是调用成功。数据库的版本总是相同的(它是一个全局变量)。我知道为了进入 onupgradeneed 版本必须始终相同。就
在我的应用中,用户应该能够动态创建和删除 objectStores。感谢indexedDB,这个操作(这到底是为什么)只被允许在onupgradeneeded中。 触发onupgradedneeded
我的代码如下(通常是众所周知的对象的命名约定): var DBOpenRequest = window.indexedDB.open("messages", 6); //... DBOpenReque
我收到的错误是: Uncaught InvalidStateError: Failed to execute 'createObjectStore' on 'IDBDatabase': The dat
多个 IndexedDB 文档讲述了以下初始化数据库和创建一个 ObjectStore 的顺序。 1) request = window.indexedDB.open(...) 2) request.
我正在尝试使用 IndexedDB 获取本地存储的一些数据。下面我有一个简单的示例,我试图用它来触发 onupgradeneeded 事件 var indexedDB =
我正在使用 IndexedDB对于一个测试项目。下面是一些示例代码: var indexedDB = window.indexedDB || window.webkitIndexedDB
我在使用 IndexedDB 时遇到问题。在 Firefox 18 上,当我创建一个新数据库时,onsuccess 方法被同时调用了 onupgradeneeded。在 Chrome 24 上(这是我
已经有这样的问答 Add Index to Pre-Existing ObjectStore In IndexedDB Using Javascript 但我看不出该代码如何满足 setVersion
我是一名优秀的程序员,十分优秀!