您好我正在尝试在IndexedDB中存储三个字段。它显示了浏览器中每个索引的内容,内容2和内容3的名称。但是,数据只保存在content3中?
这是我的源代码:
<script type="text/javascript">
var request = indexedDB.open("synker");
var db;
request.onupgradeneeded = function() {
// The database did not previously exist, so create object stores and indexes.
db = request.result;
var store = db.createObjectStore("notes", {keyPath: "ID"});
store.createIndex("content","content", { unique: false });
store.createIndex("content2","content2", { unique: false });
store.createIndex("content3","content3", { unique: false });
};
request.onsuccess = function() {
db = request.result;
};
function addData(data) {
var tx = db.transaction("notes", "readwrite");
var store = tx.objectStore("notes");
store.put({content: data, ID:1});
store.put({content2: data, ID:1});
store.put({content3: data, ID:1});
}
答案 0 :(得分:2)
每次调用store.put都会存储一个单独的对象。对象是属性的集合。索引是一种数据结构,它操作多个对象的属性。
您可能只想使用一次对store.put的调用来存储具有多个属性的单个对象。
function addData(data) {
var tx = ...;
var store = ...;
// One call to put that stores all three properties together in one object.
store.put({'content': data, 'content2': data, 'content3': data});
}