如何在 IndexedDB 中按键获取()值?

How to get() in IndexedDB the value by key?

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

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

我认为该代码目前有效,因为我在调试我的程序时得到了它:

get() 方法正在提取内容。但我希望变量 mystorage 中的值应该只是 IndexedDB 条目的值。

到目前为止我所做的确实解决了这个问题: - 阅读有关 get()-method in indexedDB 的文档 - 在 Whosebug 上有一个 post 讨论了这个问题。但我无法用它解决问题

我自己解决了这个问题:

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

使用此代码,您可以获取 indexedDB 的键 4 的值。