IndexedDB 错误:未找到错误:无法在'IDBDatabase'上执行'transaction':找不到其中一个指定的对象存放区

y4ekin9u  于 2022-12-09  发布在  IndexedDB
关注(0)|答案(3)|浏览(166)

我已经在我的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"})

},[])

}
0x6upsns

0x6upsns1#

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

将此行置于useEffect()中
代码应为

let userSearchDB,userSearchDBstore,userSearchDBtx,userDbrequest;

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

此外,在执行此操作时还应递增版本

p8h8hvxi

p8h8hvxi2#

您在连接数据库之前调用了userSearchDBstore.put(...),这意味着没有完成保证升级所需的回调,这意味着没有创建保证存储。
您需要等待。请将put调用移到open db请求成功回调中,以便它仅在数据库升级后运行。

bwntbbo3

bwntbbo33#

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

相关问题