简体   繁体   中英

Failed to execute 'createObjectStore' on 'IDBDatabase'

why do I have a error ?

My code:

var idb = window.indexedDB ||      // Use the standard DB API
          window.mozIndexedDB ||   // Or Firefox's early version of it
          window.webkitIndexedDB;  // Or Chrome's early version

var IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction;
var IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange;

var dbName='nameDb';

var idbRequest=idb.open(dbName,'4.67' /*,dbDescription  */);

idbRequest.onsuccess=function (e) {
    debugger

    var db=e.target.result; 

    if (!db.objectStoreNames.contains('chat')){
        co=db.createObjectStore('chat',{'id':100});
    };

    if (!db.objectStoreNames.contains('iam')){
        co1=db.createObjectStore('iam');
    }; 
};

idbRequest.onerror = function (e) {
    debugger
};

Uncaught InvalidStateError: Failed to execute 'createObjectStore' on 'IDBDatabase': The database is not running a version change transaction. index.html:37 idbRequest.onsuccess

You can't create an objectStore in an onsuccess method. You can only do this in a upgradeneeded event.

Quote from docs:

When you create a new database or increase the version number of an existing database (by specifying a higher version number than you did previously, when Opening a database), the onupgradeneeded event will be triggered. In the handler for this event, you should create the object stores needed for this version of the database

See documentation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM