Reputation: 149
I write codes to create database:
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));
};
It runs fine in FF/Chrome, the code: JSON.stringify(db) returns json object. But, it doesn't work in IE10. The code: JSON.stringify(db) returns an empty object.
Do everybody have the same problem? Could you spent your time to help me please? Thanks.
Update: I also checked IndexedDB supported in IE10, like it:
var indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.msIndexedDB;
It returns true! I don't know JSON.stringify(db) always returns an empty object. :(
Upvotes: 1
Views: 621
Reputation: 490
Well, your indexed DB is acctually defined, that is also why you got a true from
var indexedDB = window.indexedDB
The issue is caused by the JSON.stringify(), it looks for a toJSON() method on any object it is asked to serialize. The variable db doesn't have it, and db.toString() is called.
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));
};
Upvotes: 1