加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 为什么db.transaction不能与indexeddb一起使用?

发布时间:2020-12-13 13:41:27 所属栏目:PHP教程 来源:网络整理
导读:我是使用inxededdb的新手,我试图从商店中获取数据.存储包含数据,但由于某种原因,代码在尝试设置var tx后停止.如果我遗失任何内容,请告诉我.以下是我试图获取该书的功能: function getBook(){ var tx = db.transaction("book","readonly"); var store = tx.o
我是使用inxededdb的新手,我试图从商店中获取数据.存储包含数据,但由于某种原因,代码在尝试设置var tx后停止.如果我遗失任何内容,请告诉我.以下是我试图获取该书的功能:
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);
      }
    };
}

解决版本:

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

    };
}
问题可能是你的db变量.您可能正在访问连接对象的已关闭或null实例.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读