IndexedDB - 提供给操作的数据不符合要求

时间:2017-09-29 10:32:30

标签: javascript json indexeddb

我正在尝试将数据存储到IndexedDB中,但我遇到了问题。当尝试添加格式良好的JSON数据时,我收到错误:

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数据 - 在'value'参数下传递给函数。 (修改后的数据,因为它是感觉上的,但是采用这种格式。)

[{
    "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"
}]

1 个答案:

答案 0 :(得分:0)

您是将值作为字符串传递还是解析为数组?

如果是字符串,那么问题是生成的密钥(c / o .filter('genre', function() { return function(input) { input = input || ''; var out = ''; switch(input) { case 1: out = 'horror'; break; case 2: out = 'comedy'; break; default: out = input; } return out; }; }) )无法分配给值(c / o autoIncrement: true),因为您无法添加属性到字符串。

更简单的重复:

keyPath: "id"