簡體   English   中英

IndexedDB 錯誤:NotFoundError:無法在“IDBDatabase”上執行“事務”:未找到指定的對象存儲之一

[英]IndexedDB Error : NotFoundError : Failed to execute 'transaction' on 'IDBDatabase': One of the specified object stores was not found

我在 ReactJs PWA 中創建了一個 IndexedDB 設置。

拋出以下錯誤:

NotFoundError: Failed to execute 'transaction' on 'IDBDatabase': One of the specified object stores was not found.

在行userSearchDBtx = userSearchDB.transaction("userdata","readwrite")

我的函數如下所示:

function Users() {
  let userSearchDB,userSearchDBstore,userSearchDBtx;
  let userDbrequest = window.indexedDB.open("MyTestDatabase", 1);

useEffect(()=>{
  if (!window.indexedDB) {
      console.log("Your browser doesn't support a stable version of IndexedDB. Such and such feature will not be available.");
  }

  else console.log("IndexedDB supported")

  userDbrequest.onupgradeneeded = function(event) { 
    userSearchDB = event.target.result;
    userSearchDBtx = event.target.transaction;
    userSearchDBstore = userSearchDB.createObjectStore("userdata",{keyPath:"uid"})
  };

  userDbrequest.onerror = function(event) {
    console.log("ERROR: " + event.target.errorCode)
  };

  userDbrequest.onsuccess = function(event) {
    console.log("IndexdDb Working")
    userSearchDB = event.target.result
    userSearchDBtx = userSearchDB.transaction("userdata","readwrite");
    userSearchDBstore = userSearchDBtx.objectStore("userdata");
  };
  //Here also put is unknown
  userSearchDBstore.put({uid:3854238974,phone:3257777,name:"shan",email:"1@1.com"})

},[])

}
let userDbrequest = window.indexedDB.open("MyTestDatabase", 1);

將此行放在 useEffect() 中

代碼應該是

let userSearchDB,userSearchDBstore,userSearchDBtx,userDbrequest;

useEffect(()=>{
   userDbrequest = window.indexedDB.open("MyTestDatabase", 2);
   ...Conrinue your code
},[])

此外,在這樣做的同時增加版本

您在連接數據庫之前調用userSearchDBstore.put(...) ,這意味着不保證升級需要回調完成,這意味着不保證存儲創建。

你需要等待。 將 put 調用移動到 open db 請求成功回調中,以便它僅在數據庫升級后運行。

我發現原因是錯誤的 storeNames 在: const request = db .transaction(storeNames , "readwrite")

暫無
暫無

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

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