我是新來的使用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