2017-05-16 3 views
1

私のコードがあるonupgradeneeded存在します私はエラーを取得する:入手オブジェクトストアは、すでに(通常は、よく知られたオブジェクトのための命名規則)内の以下のよう

Failed to execute 'createObjectStore' on 'IDBDatabase': An object store with the specified name already exists. at IDBOpenDBRequest.DBOpenRequest.onupgradeneeded

が空であるデータベースの新しいバージョンで動作createObjectStoreはありませんか?エラーを修正するにはどうすればよいですか?

私はdbオブジェクトをログに起こったと詳細は以下の通りです:

enter image description here

バージョン番号はサマリ行とするとき、拡張が異なる理由を、私は好奇心旺盛です。

答えて

6

Isn't the createObjectStore operating on a new version of the database which is empty?

upgradeneededを取得すると、データベースは前の状態になります。ユーザーがどのバージョンのコードを訪問したか分からないので、それが何だったのかを知るには、そのイベントのoldVersionを調べる必要があります。 1は微妙です

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.tx.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 back4となりました。

+0

ブラウザが既に以前のバージョンを開いていた場合、データベースは空ではありません。 –

関連する問題