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

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 调用移动到打开数据库请求成功回调中,以便它仅在数据库升级后运行。

我发现原因是商店名称错误:const request = db .transaction(storeNames , "readwrite")