HelloWoRlD
HelloWoRlD

Reputation: 139

Storing data in IndexedDB

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

Upvotes: 3

Views: 6357

Answers (1)

Josh
Josh

Reputation: 18700

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

Upvotes: 5

Related Questions