我刚开始使用 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 变量上。您可能正在访问连接对象的已关闭或空实例。
尝试在函数内部创建数据库连接。不要使用全局数据库变量。
关于javascript - 为什么 db.transaction 不能与 indexeddb 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22148299/