本文介绍了为什么db.transaction不能用于indexeddb?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是使用inxededdb的新手,并试图从商店获取数据。商店包含数据,但由于某些原因,代码在尝试设置var tx后停止。如果我缺少任何东西,请告诉我。这里是我试图拿到这本书的函数:

pre $函数getBook(){
var tx = db.transaction(book,readonly);
var store = tx.objectStore(book);
var index = store.index(by_getid);

var request = index.get(<?php echo $ _GET ['book']?>);
request.onsuccess = function(){
var matching = request.result;
if(matching!== undefined){
document.getElementById(text-container)。innerHTML = matching.text;
} else {
alert('no match');
report(null);
}
};





解决的版本:

  function getBook(){
var db;
var request = indexedDB.open(library,1);

request.onsuccess = function(evt){
db = request.result;
var transaction = db.transaction([book]);
var objectStore = transaction.objectStore(book);
var requesttrans = objectStore.get(<?php echo $ _GET ['book']?>);

requesttrans.onerror =函数(事件){

};

requesttrans.onsuccess = function(event){
alert(requesttrans.result.text);
};

};
}


解决方案

变量。您可能正在访问连接对象的关闭或空实例。



尝试在函数内部创建数据库连接。不要使用全局数据库变量。


I am new at using inxededdb and am trying to get data out of a store. The store contains data, but for some reason the code stops after trying to set the var tx. If I am missing anything please let me know. Here is the function with which I am trying to get the book:

function getBook(){
    var tx = db.transaction("book", "readonly");
    var store = tx.objectStore("book");
    var index = store.index("by_getid");

    var request = index.get("<?php echo $_GET['book'] ?>");
    request.onsuccess = function() {
      var matching = request.result;
      if (matching !== undefined) {
         document.getElementById("text-container").innerHTML = matching.text;
      } else {
        alert('no match');
        report(null);
      }
    };
}

Solved Version:

function getBook(){
    var db;
    var request = indexedDB.open("library", 1);

    request.onsuccess = function (evt) {
    db = request.result;
    var transaction = db.transaction(["book"]);
    var objectStore = transaction.objectStore("book");
    var requesttrans = objectStore.get(<?php echo $_GET['book'] ?>);

        requesttrans.onerror = function(event) {

        };

        requesttrans.onsuccess = function(event) {
            alert(requesttrans.result.text);
        };

    };
}
解决方案

The problem is probably your db variable. You are probably accessing a closed or null instance of a connection object.

Try instead to create the db connection right inside the function. Do NOT use a global db variable.

这篇关于为什么db.transaction不能用于indexeddb?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 16:18