我试图在 IndexedDb 添加事务的 onsuccess 回调中有一个函数,但由于某种原因,永远不会调用 onsuccess 回调。我基本上尝试将电影对象添加到 IndexedDb 并在回调中尝试通过迭代光标来显示 indexedDb 中的所有电影。
我希望新添加的电影也将显示。但回调失败。下面是我的代码。有人可以让我知道是什么问题吗?
var movieName=document.getElementById('movieInput').value;
var movieDataToStore = [{ movieid: "5", name: movieName, runtime:"60"}];
var request = indexedDB.open("movies", 1);
request.onsuccess = function(event) {
db = event.target.result;
//var transaction = window.db.transaction(["movies"], "readwrite");
//alert(db.transaction("movies").objectStore("movies").add(null));
var requestDataadd=window.db.transaction(["movies"],"readwrite").objectStore("movies").add(movieDataToStore[0]);
requestDataadd.onsuccess = function(event) {
window.db.transaction("movies").objectStore("movies").openCursor().onsuccess = function(event) {
var cursor = event.target.result;
if (cursor) {
alert("CURSOR: movie: " + cursor.key + " has name " + cursor.value.name);
cursor.continue();
} else {//writeLog("CURSOR: No more entries!");
alert("Cursor at the Load Button unabe to open");
}
};
};
};