我对 indexeddb 有疑问 - 我正在使用此方法打开和设置数据库
DBService.prototype.open = function (dbName, entity, cb) {
var self = this;
var req = indexedDB.open(dbName)
req.onsuccess = function(e) {
var v = 1;
DBService.storage.db = e.target.result;
var db = DBService.storage.db;
// We can only create Object stores in a setVersion transaction;
if (v != db.version) {
var setVrequest = db.setVersion(v);
setVrequest.onerror = function (err, stack) {
console.log(err, stack);
}
setVrequest.onsuccess = function(e) {
if(db.objectStoreNames.contains(entity)) {
db.deleteObjectStore(entity);
}
var store = db.createObjectStore(entity,
{keyPath: "timeStamp"});
e.target.transaction.oncomplete = function() {
console.log(db);
cb(db);
};
};
} else {
cb(db);
/*
req.transaction.oncomplete = function() {
cb(db);
*/
}
};
req.onerror = function (err, stack) {
console.log(err)
console.log(stack)
}
};
一切都很好,除非数据库从未打开过。我仍然得到
InvalidStateError: DOM IDBDatabase Exception 11
(当我 console.log req.error 时)
我知道这段代码现在很脏,我正在尝试一切。我保证当它运行良好时,我会重构它!
谢谢!