3
我试图将数据存储到IndexedDB中,但遇到了问题。当尝试添加格式正确的JSON数据出现错误:IndexedDB - 提供给操作的数据不符合要求
DataError: Data provided to an operation does not meet requirements.
下面是我的IndexedDB的代码:
function dbInit(key, value) {
// Open (or create) the database
var open = indexedDB.open("MyDatabase", 6);
// Create the schema
open.onupgradeneeded = function() {
var db = open.result;
var store = db.createObjectStore("MyObjectStore", {keyPath: "id", autoIncrement: true});
var index = store.createIndex("types", "types");
};
open.onsuccess = function() {
// Start a new transaction
var db = open.result;
var tx = db.transaction("MyObjectStore", "readwrite");
var store = tx.objectStore("MyObjectStore");
var index = store.index("types");
// Add some data
store.put(value);
// Close the db when the transaction is done
tx.oncomplete = function() {
db.close();
};
}
}
通过我注意到一些类似的问题,寻找后,这个问题是存在,如果一个keyPath没有被指定,但在下面我已经指定keyPath为“id”和autoIncrement:true,所以这应该作为keyPath。
下面是我试图添加的JSON数据 - 通过'值'参数下的函数。 (修改后的数据,因为它是sensetitive,但这种格式。)
[{
"code": 1,
"content": "XXX.html",
"scheme": 4,
"type": "XXX"
}, {
"code": 2,
"content": "XXX.html",
"scheme": 6,
"type": "XXX"
}, {
"code": 3,
"content": "XXX.html",
"scheme": 1,
"type": "XXX"
}, {
"code": 4,
"content": "XXX.html",
"scheme": 4,
"type": "XXX"
}]
我看不出有任何麻烦。 [jsfiddle示例](https://jsfiddle.net/4o2obfe0/1/)。我已经使用了你的函数和[有](https://imgur.com/a/E3QWS)结果截图(你可以在Chrome DevTools Application选项卡上看到自己)。同时检查你的JSON,结尾处有'}'而不是']' –
有趣。也许这是一个Firefox问题?我每次都遇到这个错误,不幸的是,这是一个Firefox应用程序,不会在Chrome上运行。刚刚注意到JSON末尾的},编辑堆栈溢出的JSON时,这只是一个错字,并且是在实时代码中的]。 –
嗯,没有任何插件的Firefox [给我同样的结果](https://imgur.com/a/EGmKz)(操作系统:Windows 10)。 –