我正在打开一个数据库,然后在其中执行一个事务。这是代码
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 ,其目的是创建一个事务并从数据库获取值
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;
我仍然无法弄清楚警报语句如何导致空数据库属性转换为 IDBDatabase 对象。 我尝试过console.log,用延迟代替alert,但没有任何效果,就像alert() 造成了奇迹。
仅供引用:我在alert()之前和之后都做了console.log(OLC.indexedDB.db) 在alert()之前它是null,在alert()之后它是一个IDBDatabase对象
最佳答案
OLC.indexedDB.db 属性为 null,因为调用第二个函数时数据库尚未创建。数据库打开是通过回调函数执行的,而其余代码则按顺序执行。您需要修改代码,以便在 dbOpenRequest.onsuccess 函数末尾执行 loadOfflineMails 函数。
更改此代码:
function _init() {
OLC.indexedDB.open();
OLC.indexedDB.loadOfflineMails();
}
进入此:
function _init() {
OLC.indexedDB.open(null,OLC.indexedDB.loadOfflineMails);
}
将第二个函数作为参数传递给打开数据库函数,这样它将在数据库初始化时执行。
并且您需要像这样修改 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
}
}
}
关于javascript - 在indexedDB中启动事务时为"TypeError: db is null",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12498412/