首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法在IndexedDB中创建IE10数据库

无法在IndexedDB中创建IE10数据库
EN

Stack Overflow用户
提问于 2014-04-25 07:52:06
回答 1查看 573关注 0票数 1

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

代码语言:javascript
复制
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中支持的IE10,如下所示:

代码语言:javascript
复制
var indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.msIndexedDB;

它还真!我不知道JSON.stringify(db)总是返回一个空对象。:(

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-03 08:49:42

嗯,您的索引DB是按实际定义的,这也是为什么您从

代码语言:javascript
复制
var indexedDB = window.indexedDB

这个问题是由JSON.stringify()引起的,它在请求序列化的任何对象上查找toJSON()方法。变量db没有它,因此调用了db.toString()。

代码语言:javascript
复制
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));
};
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23287253

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档