Getting objects
Retrieving an object that you've previously stored is pretty easy, provided you know the key. If you only know a value that is from an index, things become a little bit harder (though not terribly so). We'll discuss the latter case later.
Note
The snippets in this section are located at snippets/07/ex5-get-objects/*
in the code package of this book. When using the interactive snippet playground, select 7: IndexedDB and examples 5a and 5b.
As with storing objects, you first need to obtain a transaction. However, since we only need a read-only transaction, it becomes much easier to do. Take a look at the following example:
// Example Snippet 5a let req = db.transaction(["definition"]).objectStore("definition") .get("02124272");
The value passed to get
is the wordNetRef
number, which is the key for the object. If you are using autogenerated keys, you'd specify them instead (though you might not know the key it off-hand, of course).
Once the object is located, a success...