4
我已經將數據存儲在indexeddb中,並且我試圖在服務人員中訪問這些數據。但是請求打開indexeddb沒有完成,它正在等待。因此,我無法訪問數據。需要幫助解決問題。下面是我的代碼來訪問服務人員的數據。無法從服務人員訪問indexeddb
var idbSupported = false;
var db;
self.addEventListener('push', function(){
if("indexedDB" in self) {
idbSupported = true;
}else{ console.log('Indexed db not supported');}
if(idbSupported) {
var openRequest = indexedDB.open("test",1);
openRequest.onupgradeneeded = function(e) {
console.log("Upgrading...");
var thisDB = e.target.result;
if(!thisDB.objectStoreNames.contains("users")) {
thisDB.createObjectStore("users", { autoIncrement: true });
}
}
openRequest.onsuccess = function(e) {
console.log("Success!");
db = e.target.result;
readUser();
}
openRequest.onerror = function(e) {
console.log("Error");
console.dir(e);
}
}
},false);
function readUser(){
var transaction = db.transaction(["users"], "readwrite");
var store = transaction.objectStore("users");
var request = store.get(user_id);
request.onerror = function() {
console.log("Error");
}
request.onsuccess = function() {
console.log("Yolo! Did it");
}
}