如何通过键获取IndexedDB中的值?

问题描述 投票:-1回答:1

我想从indexedDB中获取key定义的条目的值。我想要提取值的代码如下所示:

request.onsuccess = function (event) {
      db = request.result;
      transaction = db.transaction("Off-DB", "readwrite");
      store = transaction.objectStore("Off-DB");

      db.onerror = function(event){
        console.log("ERROR" + event.target.errorCode);
      }

      store.put({value: data});

      var mystorage = store.get(1);

      transaction.complete = function() {
        db.close();
      }
    }

我认为该代码到目前为止工作,因为我解决了调试我的程序:

Browser Screenshot

get() - Method正在提取一些东西。但我想在变量mystorage中应该只是IndexedDB条目的值。

到目前为止我所做的事情确实解决了这个问题: - 读取有关get() - indexedDB中的方法的文档 - stackoverflow上有一篇文章讨论了这个问题。但我无法解决它的问题

javascript indexeddb
1个回答
-1
投票

我自己解决了这个问题:

          db.transaction("Off-DB").objectStore("Off-DB").get(4).onsuccess = function(event) {
        console.log("Your value is:" + event.target.result.value);
      };

使用此代码,您将获得indexedDB的key 4的值。

© www.soinside.com 2019 - 2024. All rights reserved.