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

任何建议都会有很大帮助。提前致谢!!!

4

1 回答 1

0

The problem looks like it is related to the line:

var db = myapp.indexedDB.db;

You need to connect to the database and then do the work in the callback. Briefly, something like:

// 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);
    }
}
于 2013-04-18T17:26:29.600 回答