我的代码有些麻烦,希望你能帮助我。我把我的.open和所有它的代码(unupgradeneede,onsuccess,onerror aso。)放在一个js文件中然后我从另一个js文件中调用它。但看起来像开源的CodeedDB和创建对象存储的代码永远不会执行。这是代码
window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
contosoData = {
db: null,
useDb: function (successCallback) {
var request = window.indexedDB.open("ContosoData", 2);
request.onsuccess = function (e) {
contosoData.db = request.result;
console.log('indexeddb.success');
successCallback();
};
request.onupgradeneeded = function (e) {
console.log('indexeddb.upgradeneeded');
contosoData.db = e.target.result;
if (contosoData.db.objectStoreNames.contains("bookingStore")) {
contosoData.db.deleteObjectStore("bookingStore");
var bookingStore = contosoData.db.createObjectStore("bookingsStore", { keyPath: "id", autoIncrement: true });
bookingStore.createIndex = ('title', 'title', { unique: false });
bookingstore.createIndex = ('id', 'id', { unique: true });
}
else if (contosoData.db.objectStoreNames.contains("workerStore")) {
contosoData.db.deleteObjectStore("workerStore");
var workerStore = contosoData.db.createObjectStore("workerStore", { keyPath: "employeeNr" });
}
else if (contosoData.db.objectStoreNames.contains("customerStore")) {
contosoData.db.deleteObjectStore("customerStore");
var customerStore = contosoData.db.createObjectStore("customerStore", { keyPath: "orgNr" });
}
var machineStore = contosoData.db.objectStoreNames.contains("machineStore", { keyPath: "machineNr" });
contosoData.db.deleteObjectStore("machineStore");
var machineStore = contosoData.db.createObjectStore("machineStore");
};
request.onerror = function (e) {
console.log('indexeddb.onerror: ' + e);
};
request.onblocked = function (e) {
console.log('indexeddb.locked');
};
}
};
contosoData.useDb(function (db) {
var transaction = contosoData.db.transaction(['bookingsStore'], 'readwrite');
var objectStore = transaction.objectStore('bookingsStore');
console.log(objectStore.name);
});
然后,当我尝试运行它时,当我尝试在另一个js文件中创建一个事务时,我得到一个未定义的事务...我已经检查了我的js文件与我打开的函数IndexedDB以正确的方式链接到html文件。我还提出了一些破坏点,看起来useDb代码永远不会执行。这是代码
function init() {
contosoData.useDb();
calendar.createCalendar();
}
calendar.createCalendar = function () {
var transaction = contosoData.db.transaction(["bookingStore"], "readwrite");
var bookingStore = transaction.objectStore("bookingStore");
}
有什么我想念的吗?我搜索了它,我试图找到一个解决方案但到目前为止没有成功......
答案 0 :(得分:0)
这会是这样吗
function init() {
calendar.createCalendar();
}
calendar.createCalendar = function () {
var transaction = contosoData.db.transaction(["bookingStore"], "readwrite");
var bookingStore = transaction.objectStore("bookingStore");
}
contosoData.useDb(init);
答案 1 :(得分:0)
我已经在您的代码中看到了两个问题。
首先,数据库始终使用版本2打开。如果您已经拥有版本2的数据库,则永远不会触发onupgradeneeded事件。您需要先检查(例如在Chrome中,按Ctrl-Shift-I并在资源下检查)。
其次,代码使用此逻辑
if (contosoData.db.objectStoreNames.contains("bookingStore")) {
contosoData.db.deleteObjectStore("bookingStore");
var bookingStore = contosoData.db.createObjectStore("bookingsStore", { keyPath: "id", autoIncrement: true });
bookingStore.createIndex = ('title', 'title', { unique: false });
bookingstore.createIndex = ('id', 'id', { unique: true });
}
对于空数据库,它肯定不会有任何对象存储,因此if块总是被评估为false。永远不会创建对象存储,因为它们位于always-false if块中。