Read Data from JavaScript IndexedDB

The indexedDB database displays the data from the table or database. It is used to get key information, values, and all content from the database.

There are the following methods and functions to read data from javascript indexedDB.

  • Read an object store's key contents.
  • Read an object store's index contents.
  • Read an object store's entire contents.

Read an object store's key contents.

Use the object store's get() method to read and get an object based on its key. The getNamesById() function below finds a Name based on an ID.

Syntax

The following method and functions read the key data of the object store.

  • The create function to get key information with the id.
  • The "onsuccess" event shows id data in the table format.
  • The "30" is the id of the indexedDB database to get information.
  • If the id is unavailable, an error event occurs and gets an error function.

Examples

The examples show available data and do not show available data with success and error events.

Example 1

The example shows the key value of the object store using the javascript function. If the id of indexedDB is available, then the success event shows the output. If the id is unavailable, then the error event shows the output.

Output

The following image shows the read data with the index id with a successful event.

Read Data from JavaScript IndexedDB

Example 2

The example shows the key value of the object store using the javascript get() function. If the id of the indexedDB database does not become available, then an error event shows the output. We can use an error event or error message as an output.

Output

The following image shows the read data with the index id with an error event.

Read Data from JavaScript IndexedDB

Read an object store's index contents.

Use the object stores in the index() method to read and get the indexed column value. The getNamesByRank() function below finds the rank of the object.

Syntax

The following method and functions read the key data of the object store.

  • The create function to get key information with the id.
  • function getNamesByRank(db_variable, rank) {
  • The "onsuccess" event shows index column data in the table format.

Examples

The example shows the index value of the object store using the javascript function. If the rank of indexedDB is available, then the success event shows the output. If the id is unavailable, then an error event shows the output.

Output

The following output image shows the required information from the database.

Read Data from JavaScript IndexedDB

Read an object store's whole contents.

The indexedDB database reads and displays available data using the javascript function, events, and methods.

Syntax

The following demonstrates how to read every object from the Contacts object storage using a cursor:

Explanation

  • The cursor returned by objectStore.openCursor() can be used to loop through an object store.
  • You must designate an onsuccess handler to use the cursor to loop across the objects in an object store.
  • The cursor is returned by event.target.result. You can use the cursor to retrieve the data.
  • The object store's cursor is moved forward by the continue() method to the location of the available record.
  • To display all information from the Contacts object storage, the following invokes the getAllContacts() function in the onsuccess event handler.

Example

The example shows the browser's support and displays data to the IndexedDb with an event called by javascript. The function uses an insert event to store multiple data and shows all data available in the database.

Output

The following image shows the read data with the data variable.

Read Data from JavaScript IndexedDB

Conclusion

The indexedDB database gives a method to read data using index, key and database name. We can read and display whole and specific data as per requirement.