簡體   English   中英

無法在“IDBDatabase”上執行“createObjectStore”

[英]Failed to execute 'createObjectStore' on 'IDBDatabase'

為什么我有錯誤?

我的代碼:

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
};

未捕獲的 InvalidStateError:無法在“IDBDatabase”上執行“createObjectStore”:數據庫未運行版本更改事務。 index.html:37 idbRequest.onsuccess

您不能創建一個objectStore在一個onsuccess方法。 您只能在upgradeneeded事件中執行此upgradeneeded

來自文檔的引用:

當您創建新數據庫或增加現有數據庫的版本號時(通過指定比以前更高的版本號,在打開數據庫時),將觸發onupgradeneeded事件。 在此事件的處理程序中,您應該創建此版本數據庫所需的對象存儲

請參閱文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM