0

I am trying to get the data stored in my ObjectStore and I want this synchronously. So instead of using onsuccess I want to use await / async.

I have implemented this below code but somehow its not returning me the data.

        async function viewNotes() {

                const tx = db.transaction("personal_notes","readonly")
                const pNotes = tx.objectStore("personal_notes")


                const items = await db.transaction("personal_notes").objectStore("personal_notes").getAllKeys()
                
                console.log("And the Items are ", items.result)


                let NotesHere = await pNotes.getAll().onsuccess

                 console.log("Ans this are the logs", NotesHere)

        }

Neither I am getting the data through items.result nor from NotesHere. When I view from debug mode, items's readyState is still in pending even after using await.

What am I missing ?

Ashish Bhoya
  • 9
  • 1
  • 3
  • Does this answer your question? [Using IndexedDB asynchronously](https://stackoverflow.com/questions/41586400/using-indexeddb-asynchronously) – Jan Derk Dec 12 '22 at 10:29

1 Answers1

2

The IndexedDB API does not natively support async/await. You need to either manually wrap the event handlers in promises, or (much better solution) use a library like https://github.com/jakearchibald/idb that does it for you.

dumbmatter
  • 9,351
  • 7
  • 41
  • 80