可以't在IE10中创建IndexedDB数据库

Can't create IndexedDB database in IE10

本文关键字:创建 IndexedDB 数据库 IE10 可以      更新时间:2023-09-26

我编写代码来创建数据库:

var db;
var request = indexedDB.open("TestDatabase");
request.onerror = function(evt) {
  console.log("Database error code: " + evt.target.errorCode);
};
request.onsuccess = function(evt) {
  db = request.result;
  console.log(JSON.stringify(db));
};

它在FF/Chrome中运行良好,代码为:JSON.stringfy(db)返回JSON对象。但是,它在IE10中不起作用。代码:JSON.stringfy(db)返回一个空对象。

每个人都有同样的问题吗?你能花点时间帮我吗?谢谢

更新:我还检查了IE10中支持的IndexedDB,比如:

var indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.msIndexedDB;

结果是真的!我不知道JSON。stringify(db)总是返回一个空对象。:(

好吧,你的索引数据库是按实际定义的,这也是为什么你从得到了一个true

var indexedDB = window.indexedDB

这个问题是由JSON.stringfy()引起的,它在被要求序列化的任何对象上寻找toJSON()方法。变量db没有它,并且调用了db.toString()。

window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
var db;
var request = window.indexedDB.open("TestDatabase",1);
request.onerror = function(evt) {
  console.log("Database error code: " + evt.target.errorCode);
};
request.onsuccess = function(evt) {
  db = request.result;
  // extend the toJSON property for your indexeddb object
  db.toJSON = function() {
     return JSON.stringify({name : db.name});
  };
  //output: [object IDBDatabase]{constructor: IDBDatabase {...}, name: "TestDatabase", objectStoreNames: DOMStringList {...}, onabort: null, onerror: null, version: 1}
  console.log(db);
  // name is a inherit property 
  console.log(db.hasOwnProperty(name));
  // name is not a numerable property
  console.log(db.propertyIsEnumerable(name));
  // toString returns a native object, not a JSON String, thats why you have {} with JSON.stringify(db)
  console.log(db.toString);
  // JSON.stringify call the db.toJSON(), and get the "{'"name'":'"TestDatabase'"}"
  console.log(JSON.stringify(db));
};