2017-09-29 5 views
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データです。 '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" 
}] 
+2

私はどんなトラブルが表示されません。 [jsfiddle example](https://jsfiddle.net/4o2obfe0/1/)。私はあなたの関数を使用しました。結果の(https://imgur.com/a/E3QWS)スクリーンショットがあります(あなたはChrome DevToolsアプリケーションタブで自分自身を見ることができます)。あなたのJSONもチェックしてください。末尾に ']'の代わりに '}'があります。 –

+2

面白いです。おそらくこれはFirefoxの問題ですか?毎回このエラーが発生します。残念ながら、これはFirefoxアプリケーションであり、Chromeでは実行されません。ちょうどJSONの終わりに気づいた、これはスタックオーバーフローのためのJSONを編集するときのちょうどタイプミスであり、ライブコードの中にある]。 –

+1

hm、プラグインなしのfirefox [同じ結果を得ました](https://imgur.com/a/EGmKz)(OS:Windows 10) –

答えて

0

値を文字列として渡すのか、配列に解析するのですか?

文字列の場合、文字列にプロパティを追加できないため、生成されたキー(c/o autoIncrement: true)に値(c/o keyPath: "id")を割り当てることができません。

シンプルREPRO:

indexedDB.open('k').onupgradeneeded = e => { 
    const db = e.target.result; 
    const s = db.createObjectStore('s', {keyPath: 'id', autoIncrement: true}); 
    s.put('string'); 
}; 
関連する問題