我已经在我的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"})
},[])
}
3条答案
按热度按时间0x6upsns1#
将此行置于useEffect()中
代码应为
此外,在执行此操作时还应递增版本
p8h8hvxi2#
您在连接数据库之前调用了
userSearchDBstore.put(...)
,这意味着没有完成保证升级所需的回调,这意味着没有创建保证存储。您需要等待。请将put调用移到open db请求成功回调中,以便它仅在数据库升级后运行。
bwntbbo33#
我发现原因是错误的storeNames:
const request = db .transaction(storeNames , "readwrite")