2017-04-15 42 views
0

我想在Chrome和Firefox中打開一個IndexedDB。我的代碼:Firefox和Chrome中的IndexedDB錯誤:未知錯誤和DOMException

var v = 2; 
        var request = indexedDB.open("orders", v); 


        //Firefox code for db init 
        request.onupgradeneeded = function (e) { 

        console.log("onupgradeneeded"); 
        var db = e.target.result; 
        // We can only create Object stores in a setVersion transaction; 

        if(db.objectStoreNames.contains("client")) { 
         var storeReq = db.deleteObjectStore("client"); 
        } 

    const employeeData = [ 
      { id: "00-01", name: "gopal", age: 35, email: "[email protected]" }, 
      { id: "00-02", name: "prasad", age: 32, email: "[email protected]" } 
     ]; 

      var objectStore = db.createObjectStore("client", {keyPath: "id"}); 

      for (var i in employeeData) { 
       objectStore.add(employeeData[i]); 
      } 

        } 

     request.onsuccess = function(e) { 
     var db = e.target.result; 

     console.log("Success"); 
     console.log(db); 


    var transaction = db.transaction(["client"]); 
      var objectStore = transaction.objectStore("client"); 
      var request = objectStore.get("00-02"); 

      request.onerror = function(event) { 
       alert("Unable to retrieve daa from database!"); 
      }; 

      request.onsuccess = function(event) { 
       // Do something with the request.result! 
       if(request.result) { 
        alert("Name: " + request.result.name + ", Age: " + request.result.age + ", Email: " + request.result.email); 
       } 

       else { 
        alert("Kenny couldn't be found in your database!"); 
       } 
      }; 


     }; 

     request.onerror = function(e) { 
      console.log(e); 
     }; 

在Chrome中它根本不起作用。我得到一個

DOMException: The user denied permission to access the database. 

在Firefox中,它的工作原理如上所示。但是,當我將數據庫的名稱更改爲myindexeddb時,它不再有效。我得到一個UnknownError

我不知道我在做什麼錯。

回答

1

在chrome中,轉到chrome:// settings/content並確保選中頂部單選按鈕。同時檢查「管理例外」列表以查看該域是否被明確阻止。