我写代码来创建数据库:
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.stringify(db)返回json对象。
但是,它在IE10中不起作用。代码:JSON.stringify(db)返回一个空对象。
每个人都有同样的问题吗?你可以花时间帮助我吗?谢谢。
更新:我还检查了IE10支持的IndexedDB,如下所示:
var indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.msIndexedDB;
它返回true!我不知道JSON.stringify(db)总是返回一个空对象。 :(
最佳答案
好吧,您的索引数据库是正确定义的,这也是为什么您从
var indexedDB = window.indexedDB
该问题是由JSON.stringify()引起的,它在要求序列化的任何对象上查找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));
};