在indexedDB中启动事务时,“TypeError:db为null”

时间:2012-09-19 15:55:01

标签: javascript indexeddb

我正在打开一个数据库,然后在其中进行交易。这是代码

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;

....等等

然后我有一个方法loadOfflineMails,其目的是创建一个事务并从DB获取一个值

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()一样引起了奇迹。

仅供参考:我在alert()之前和之后都完成了console.log(OLC.indexedDB.db) 在alert()之前它是null并且在alert()之后它是一个IDBDatabase对象

1 个答案:

答案 0 :(得分:3)

OLC.indexedDB.db属性为null,因为在调用第二个函数时尚未创建数据库。使用回调函数执行数据库打开,而其余代码按顺序执行。您需要修改代码,以便在dbOpenRequest.onsuccess函数结束时执行loadOfflineMails函数。

更改此代码:

function _init() {
    OLC.indexedDB.open();
    OLC.indexedDB.loadOfflineMails();
}

进入这个:

function _init() {
    OLC.indexedDB.open(null,OLC.indexedDB.loadOfflineMails);
}

传递第二个函数作为参数来打开数据库函数,这样它将在DB初始化时执行。

你需要像这样修改Open Db包装器:

OLC.indexedDB.open = function(type, callback) {
    // opening OLCMail DB
    try {
        var dbOpenRequest = window.indexedDB.open("OLCMail");
        dbOpenRequest.onsuccess = function(event){
            /*your code*/
            callback();//execute the callback function after db init is complete
        }
     }
}