我是新来的使用inxededdb,并试图从商店获取数据。商店包含数据,但由于某些原因,代码在尝试设置var tx后停止。如果我缺少任何东西,请告诉我。下面是我想获得这本书的功能:为什么db.transaction不能与indexeddb一起使用?
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);
};
};
}
它的工作:)我检查了var数据库,它是未定义我做的是我重新打开函数中的连接:) – wayzz