2013-07-15 1 views
0

일부 테스트에서는 Indexed DB API를 사용하려고합니다.NotFoundError : DOM IDBDatabase indexedDB에 트랜잭션을 생성 할 때 예외가 발생합니다.

<html> 
<head> 
    <script type="text/javascript"> 

     var db = null; 
     const dbName = "contactsDB"; 
     const dbVersion = 1; 
     const storeName = "contacts"; 

     const contacts = [ 
      {id : 1, firstname : 'F1', lastname : 'L1'}, 
      {id : 2, firstname : 'F2', lastname : 'L2'} 
     ]; 

     function init() { 
      window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB; 
      openDB(); 
     } 

     function openDB() { 
      var request = window.indexedDB.open(dbName, dbVersion); 

      request.onerror = function (e) { 
       alert('DB connexion error : ' + e.target.errorCode); 
      }; 

      request.onsuccess = function (e) { 
       alert('DB connexion success'); 
       // get db instance 
       db = e.target.result; 
      }; 

      // seulement implemente sur les browsers recents 
      request.onupgradeneeded = function (e) { 
       // updgrade DB 
       var db = e.target.result; 

       if (db.version != dbVersion) {    
        // create object store 
        var objectStore = db.createObjectStore(storeName, {keyPath : "id"}); 

        // create index to search contacts by lastname. 
        // Duplicates possible ==> so no unique index 
        objectStore.createIndex("lastname", "lastname", {unique : false}); 
       } 
      }; 
     } 

     function addToDB() { 
      // get object store in tx 
      var objectStore = getObjectStore(storeName, "readwrite"); 

      // stores values 
      for (var c in contacts) { 
       var request = objectStore.add(contacts[c]); 
       request.onsuccess = function (e) { 
        alert('Add success for ' + e.target.result); 
       } 
      } 

     } 

     function getObjectStore(store_name, mode) { 
      var tx = db.transaction(store_name, mode); 
      return tx.objectStore(store_name); 
     } 

    </script> 
</head> 
<body onload="init();"> 
    <input type="button" onclick="addToDB();" value="Add" /> 
</body> 
</html> 

내가 로컬 호스트 도메인과 페이지를 사용하는 웹 서버가 :

내 코드는 다음과 같다.

Firefox 22.0으로 페이지를로드하면 DB가 성공적으로 열립니다. 그러나, 나는 추가 버튼을 클릭 할 때 너무 addToDB 기능이 나는 파이어 폭스 콘솔에서 다음과 같은 오류가있다라고 :

NotFoundError: The operation failed because the requested database object could not be found. For example, an object store did not exist but was being opened.
var tx = db.transaction(store_name, mode);

가 나는 또한 크롬 24에서 동일한 테스트를 만들었을 때, 나는 추가 버튼을 클릭합니다 오류는 같은 줄에서 발생합니다. var tx = db.transaction (store_name, mode); 및 크롬 콘솔에서 나는 다음과 같은 오류가 있습니다

Uncaught Error: NotFoundError: DOM IDBDatabase Exception 8

그 예외에 대한 자세한 정보를 검색하여을, 나는 다음과 같은 링크를 발견 :

https://developer.mozilla.org/en-US/docs/Web/API/IDBDatabaseException?redirectlocale=en-US&redirectslug=IndexedDB%2FIDBDatabaseException

링크에서를, 그것은 예외 8 표시된 것 : 예를 들어 IDBTransaction.abort 호출을 통해 요청이 중단되었습니다.

내 문제는 왜 내 요청이 그 시점에서 중단되는지 알 수 없습니다.

누군가가 이러한 문제를 해결하는 데 도움이 될만한 아이디어가 있습니까?

감사합니다.

실뱅은

답변

1

객체 저장소가 있기 때문에 if (db.version != dbVersion) 검사의 생성되지 않습니다. 그 블록은 절대로 입력되지 않습니다. 수표를 지우고 const dbVersion = 2;으로 변경하면 모든 것이 잘되어야합니다.

관련 문제