查找索引数据库中是不是已存在值

Posted

技术标签:

【中文标题】查找索引数据库中是不是已存在值【英文标题】:Finding if a value already exists in the indexedDB查找索引数据库中是否已存在值 【发布时间】:2012-07-18 19:39:31 【问题描述】:

我在 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

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

【问题讨论】:

该错误消息表明myapp.indexedDB.dbnull,这显然会阻止您对其进行交易。你应该先解决这个问题。 【参考方案1】:

这个问题看起来和这条线有关:

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);
    

【讨论】:

以上是关于查找索引数据库中是不是已存在值的主要内容,如果未能解决你的问题,请参考以下文章

Postgres查询以查找连接表中是不是已存在组合

CodeIgniter - 检查数据库中是不是已存在值

如何检查 Unity 的 Firebase 数据库中是不是已存在键/值

查找作为列表存在的列元素的数据框索引的最快方法

数组索引是不是存在

js如何查找数组中是不是存在某个值