DB を開いて、その中でトランザクションを実行しています。ここにコードがあります
var OLC = {}
// Initialising the window.IndexedDB Object
window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
window.IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange;
window.IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction;
var db = null;
OLC.indexedDB = {};
OLC.indexedDB.db = null;
OLC.indexedDB.version = null;
OLC.indexedDB.open = function(type) {
// opening OLCMail DB
try {
var dbOpenRequest = window.indexedDB.open("OLCMail");
dbOpenRequest.onsuccess = function(event){
OLC.indexedDB.db = dbOpenRequest.result;
db = dbOpenRequest.result;
OLC.indexedDB.version = db.version;
var thisDB = db; // Need to create this variable since the variable db is assigned to other things later
db.onversionchange = function(e){
console.log("Version change triggered, so closing database connection", e.oldVersion, e.newVersion, thisDB);
thisDB.close();
};
console.log("Database Opened", db, event);
};
// Creating objectstore "MailHeaders"
dbOpenRequest.onupgradeneeded = function(e){
console.log("Database upgrade needed");
db = dbOpenRequest.result;
var transaction = dbOpenRequest.transaction;
....等々
次に、トランザクションを作成してDBから値を取得することを目的としたloadOfflineMailsメソッドがあります
OLC.indexedDB.loadOfflineMails = function () {
console.log("Opening a new transaction.");
try {
var db = OLC.indexedDB.db;
console.log(OLC.indexedDB.db);
var objectStore = db.transaction(["MailHeaders"]).objectStore("MailHeaders");
console.log("Object Store opened", objectStore);
var cursor = null;
....すぐ
私はそれを実行しています
function _init() {
OLC.indexedDB.open();
OLC.indexedDB.loadOfflineMails();
}
window.addEventListener("DOMContentLoaded", _init, false);
私が抱えている問題は、OLC.indexedDB.loadOfflineMails(); を呼び出すと、OLC.indexedDB の db プロパティが null を示しているため、トランザクションが完了していません。
しかし、loadOfflineMails() のトランザクション ステートメントの前に値をアラートすると、コードは正常に動作します。元。
OLC.indexedDB.loadOfflineMails = function () {
console.log("Opening a new transaction.");
try {
alert("ANY THING");
var db = OLC.indexedDB.db;
console.log(OLC.indexedDB.db);
var objectStore = db.transaction(["MailHeaders"]).objectStore("MailHeaders");
console.log("Object Store opened", objectStore);
var cursor = null;
アラート ステートメントがどのようにして null db プロパティを IDBDatabase オブジェクトに変換するのか、まだわかりません。私はconsole.logを試しましたが、アラートの代わりに遅延が発生しましたが、何も機能しません.alert()のようなものは奇跡を引き起こします.
参考までに: 私は console.log(OLC.indexedDB.db) を alert() の前後の両方で実行しました。