2012-07-18 44 views
0

我在IndexedDB中工作。我能夠創建,填充和刪除我的jquery移動應用程序中的值。查找索引數據庫中是否已經存在一個值

現在,當我第一次來到一個頁面時,我應該檢查值是否在我的數據庫中可用。如果是這樣,如果不存在,我需要顯示「存在」或「不存在」。我寫了下面的代碼。我已經在document.ready上調用了這個函數。

myapp.indexedDB.existsInFavourites = function(value){ 
    var db = myapp.indexedDB.db; 
    var request = db.transaction(["todo"]).objectStore("todo").get(typeid); 
    request.onerror = function(event) { 
     // Handle errors! 
    }; 
    request.onsuccess = function(event) { 
     // Do something with the request.result! 

    }; 
    } 

導致我在下面的錯誤

Uncaught TypeError : Cannot call method 'transaction' of null 

任何建議將是巨大的幫助。提前致謝!!!

+0

該錯誤信息表明,'myapp.indexedDB.db'是'null',這顯然會妨礙你做它事務。你應該先解決這個問題。 – dumbmatter 2012-07-18 19:59:24

回答

0

的問題看起來是涉及到行:

var db = myapp.indexedDB.db; 

您需要連接到數據庫,然後執行工作的回調。簡單地說,是這樣的:

// var typeid = ...; 

// Request a connection 
var openRequest = indexedDB.open(name,version); 
var openRequest.onsuccess = function(event) { 
    var db = event.target.result; 
    // event.target === this and event.target === openRequest 
    // so use whatever you prefer 

    // Now use the 'db' variable to do the work 
    // Note: removed unnecessary brackets around todo 
    var getRequest = db.transaction("todo").objectStore("todo").get(typeid); 
    getRequest.onsuccess = function(event) { 
     var myResult = event.target.value; 
     // event.target === this, and event.target === getRequest 

     console.log('Got %s !', myResult); 
    } 
} 
相關問題