2013-10-26 1 views
1

기본적으로 IndexedDB를 사용하여 일부 데이터를 저장해야하는 Firefox OS 용 응용 프로그램을 만들려고합니다.IndexedDB 참조 오류 : db가 정의되지 않았습니다.

store() 함수는 사용자가 양식을 제출 한 이름 및 설명 변수를 작성하는 제출 단추를 클릭 할 때 호출됩니다.

그러나 DB 개체가 정의되지 않았다는 오류가 계속 나타납니다. 왜 이런 일이 일어나고 있는거야? 이름과 설명을

function store() { 
    // create the transaction with 1st parameter is the list of stores and the second specifies 
    // a flag for the readwrite option 
    var transaction = db.transaction([ 'Apps' ], 'readwrite'); 

    //Create the Object to be saved i.e. our App details 
    var value = {}; 
    value.name = name; 
    value.desc = description; 

    // add the details to the store 
    var store = transaction.objectStore('Apps'); 
    var request = store.add(value); 
    request.onsuccess = function (e) { 
     alert("Your App data has been saved"); 
    }; 
    request.onerror = function (e) { 
     alert("Error in saving the App data. Reason : " + e.value); 
    } 
} 


$(document).ready(function(){ 
// variable which will hold the database connection 
var db; 

    if (window.indexedDB) { 
     console.log("IndexedDB is supported"); 
    } 
    else { 
     alert("Indexed DB is not supported!"); 
    } 

    // open the database 
    // 1st parameter : Database name. We are using the name 'Appsdb' 
    // 2nd parameter is the version of the database. 
    var request = indexedDB.open('Appsdb', 1); 

    request.onsuccess = function (e) { 
     // e.target.result has the connection to the database 
     db = e.target.result; 

     console.log(db); 
     console.log("DB Opened!"); 
    } 

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

    // this will fire when the version of the database changes 
    // We can only create Object stores in a versionchange transaction. 
    request.onupgradeneeded = function (e) { 
     // e.target.result holds the connection to database 
     db = e.target.result; 

     if (db.objectStoreNames.contains("Apps")) { 
      db.deleteObjectStore("Apps"); 
     } 

     // create a store named 'Apps' 
     // 1st parameter is the store name 
     // 2nd parameter is the key field that we can specify here. Here we have opted for autoIncrement but it could be your 
     // own provided value also. 
     var objectStore = db.createObjectStore('Apps', { keyPath: 'id', autoIncrement: true }); 

     console.log("Object Store has been created"); 
    }; 

}); 
+0

IndexedDB를 사용하는 경우 로그에 지원됩니까? –

답변

4

문제 formname.description.value NAME = formname.name.value = 설명은 db 변수의 범위에있다. 현재 var db;$(document).ready 함수 내부에 선언되어 있습니다. 선언을보다 전역적인 범위로 이동하십시오. 즉,이 함수 밖에서 변수는 store() 함수에서도 볼 수 있습니다.

희망이 도움이됩니다.

+0

감사합니다. 이것은 효과가있다! 나는 당신의 블로그에서 예제를 실험하고 있었고 가변 범위를 뒤섞었다. :-) – Sayak

2
var value = {}; 
value.name = name; 
value.desc = description; 

할당 값 : -

여기에 내 현재 코드입니다.

+0

이것은 이전에 수행 한 작업 이었지만 문제는 db 변수의 범위에 있습니다. – Sayak