Using cursors and indexes
So far, we've only retrieved data with the unique key it was stored with. But we can also use the indexes we've created to locate an object without knowing the object's unique key.
Note
The snippets in this section are located at snippets/07/ex7-cursors-indexes/*
in the code package of this book. When using the interactive snippet playground, select 7: IndexedDB and examples 7a and 7b.
In order to do so, we request an index
from the object store after we've created a transaction. Once we have an index, we can call get
to retrieve an object that is in the index. Let's see an example:
// Example Snippet 7a let req = db.transaction(["definition"]).objectStore("definition") .index("lemmas").get("cat"); req.onerror = ...; req.onsuccess = function (evt) { let definition = evt.target.result; if (definition) { console.log(JSON.stringify(definition, null, 2)); } else { console.log("Couldn't find entry."); } };
If you take a look at our definitions
...