这是我要调用的函数:
function restoreCurrent() {
var tzact = db.transaction(['Picture'],"readonly").objectStore("Picture").get(2);
tzact.onerror = function(event) {
alert ("Failed to retrieve the data.");
}
tzact.onsuccess = function(event) {
document.getElementById("demo").innerHTML += "Transaction success.<br />";
}
}
window.onload = function() {
restoreCurrent();
}
如您所见,它应该在成功或错误时输出一些信息。实际上,它不输出任何内容。之后,我在.onsuccess函数中有更多代码,但是什么也没有执行。它停止工作的那一行是数据库调用,我已经测试了很多。 作为记录,创建数据库时这两行代码
var tzact = db.transaction(['Picture'], "readwrite").objectStore("Picture").add({id : 2, pictureData : working});
以及当我尝试更新该特定记录时的这一行
var tzact = db.transaction(['Picture'], "readwrite").objectStore("Picture").put({id : 2, pictureData : working});
似乎可以执行OK或至少嵌入它们的功能可以继续输出成功消息。无法提取任何数据就很难确定。
编辑:自己弄清楚。答案是我必须将其放入每个函数中:
var request = window.indexedDB.open("[database name]",5);
request.onerror = function() {
alert ("Failed to open the database.");
}
request.onsuccess = function() {
var db = request.result;
var tzact = db.transaction(['Picture'],"readonly").objectStore("Picture").get(2); // or "readwrite" and .put(2), as the case might be
tzact.onerror=function() {
alert ("Data transaction failed.");
}
tzact.onsuccess=function() {
document.getElementById("demo").innerHTML += "Data transaction successful.<br />";
if (tzact.result) {
var working = tzact.result.pictureData;
// the rest of the function goes here
} else {
alert ("No data received.");
}
}
}