How to get objectstore from indexedDB?
Asked Answered
L

3

5

I have indexedDb on my app for web storage.

I would like to get the store form the below code.

var store = myapp.indexedDB.db.transaction(['tree_nodes'],'readwrite').objectStore('tree_nodes'); 

It returns error. I was well known of opening indexeddb database and version changing.

The error is Uncaught TypeError: Cannot call method 'transaction' of null

I was tried it with the break point. In that case it works fine without errors.

How can i get the store? please help me.

Thanks in advance!

Limburg answered 10/8, 2012 at 8:56 Comment(0)
M
9

The error is probably because your db variable is null. This is almost always because you are trying to store db in a global variable as a result of a callback, and then access the db variable in a separate function that is not guaranteed to only execute after your db variable is set, such that the browser finds you are accessing an uninitialized variable.

The solution is simple (but frustrating). You cannot use a global variable in this manner unless you want to learn about some library's implementation of promises and deferred objects. Instead, look at the answer given by Deni. Use callbacks and write your code in callback functions, not global variables. 'db' is only accessed from within the callback request.onsuccess function, and is not global. That's why Deni's will work. His code will only attempt to access db when it is guaranteed to be initialized (not null).

Since you didn't post your surrounding code, which turns out to be important, you will need to do something like this:

// I am an evil global variable that will not work as expected
myapp.indexedDB.db = 'DO NOT USE ME OR YOU WILL GET AN ERROR';

// I am a good function that only accesses an initialized db variable
function doit() {
    var request = window.indexedDB.open(......);
    request.onsuccess = function(event) {
        // Use this db variable, not your global one
        var db = event.target.result;

            // Note that you can also access the db variable using other means
        // here like this.result or request.result, but I like to use event.target
        // for clarity.

        // Now work with the db variable
        var store = db.transaction(['tree_nodes'],'readwrite').objectStore('tree_nodes');
        // do some more stuff with store....
    };
}
Mweru answered 4/5, 2013 at 21:31 Comment(2)
the global variable was it for me. I wasn't even using it, so fairly easy to remove.Archipenko
Josh, the "evil global variable" is a really good point I've seen a lot of people get errors using this approach or IDB +1Mose
M
5

Here is in short what you need to do in order to get data from indexeddb First you need to open the database in order to retrieve data.

var request = indexedDB.open("tree_nodes", v); // first step is opening the database
request.onsuccess = function(e) {
        var db =  e.target.result;
        var trans = db.transaction(["tree_nodes"], 'readwrite'); //second step is opening the object store
        var store = trans.objectStore("tree_nodes");
        
        var request = store.get(id); //getting single object by id from object store
        
        request.onsuccess = function(e) {
            showDetails(e.target.result); // data retreived
            db.close();
        };
        
        request.onerror = function(e) {
                console.log("Error Getting: ", e);
        };
};
Mose answered 10/8, 2012 at 14:59 Comment(0)
G
0

It works for me and I could get data
Visit https://dev.to/pandresdev/get-data-from-indexeddb-7hg

Grant answered 11/7, 2022 at 7:58 Comment(1)
A link to a solution is welcome, but please ensure your answer is useful without it: add context around the link so your fellow users will have some idea what it is and why it is there, then quote the most relevant part of the page you are linking to in case the target page is unavailable.Gateway

© 2022 - 2024 — McMap. All rights reserved.