2017-10-06 74 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