3

Hi I'm trying to store three fields in an IndexedDB. Its showing the names of each three indexes .. content, content2 and content3 in the browser. However, data is only being saved into content3 ?

Here is my source code:

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

1 Answer 1

5

Each call to store.put stores a separate object. An object is a collection of properties. An index is a data structure that operates off the property of several objects.

You probably want to store a single object with multiple properties, using only one call to 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});
}
1

Not the answer you're looking for? Browse other questions tagged or ask your own question.