2017-10-06 70 views
0

嗨我试图在IndexedDB中存储三个字段。它在浏览器中显示每个三个索引的名称.. content,content2和content3。但是,数据只被保存到content3中?在IndexedDB中存储数据

这里是我的源代码:

<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}); 
     } 

回答

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}); 
} 
+0

斑点我看到了,现在谢谢!有效! – Donal5