Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8497175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:03:32+00:00 2026-06-11T00:03:32+00:00

I am trying to refactor some code I wrote that uses IndexedDb. Ideally what

  • 0

I am trying to refactor some code I wrote that uses IndexedDb. Ideally what I would like to do is create a little business library that abstracts some of the ugliness of using IndexedDb. So for example I will create a toDoList object that will have some methods to Get, Add, Update, Delete, and inside those methods I will be calling IndexedDb.

Here is an example of what I have:

var MyApp = MyApp || {};

(function() {

  var req = indexedDB.open("todostore", 1);

  req.onerror = function(e) { console.log(e); };

  req.onupgradeneeded = function (e) {
    var newDB = e.target.result;
    newDB.createObjectStore("todostore", { keyPath : "id", autoIncrement : true });
  };

  req.onsuccess = function () {
    MyApp.db = req.result;
  };

})();

MyApp.todolist = (function() {
  return {
    get : function(key, success) {
      var tran = MyApp.db.transaction("todostore");
      var req = tran.objectStore("todostore").get(key);

      req.onsuccess = function (e) {           
        success(e.target.result);
      };
    }
  };
})();

//consumer of library would ideally just do something like this:

var worked = function(e) {
   //do something...
}
MyApp.todolist.get(1, worked);

The problem is MyApp.db is undefined in the get method because the onsuccess callback hasn’t been triggered yet. I am still new to javascript, so wondering what options / patterns I could use. Thanks for any help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T00:03:34+00:00Added an answer on June 11, 2026 at 12:03 am

    There are probably 1000 different ways to handle this problem. But I would suggest simply including an option for failure in your “get” method and have that triggered if the database is not ready:

    MyApp.todolist = (function() {
      return {
        get : function(key, success, failure) {
          if(!MyApp.db) { 
            if(typeof failure === "function") {
              failure("Database is not ready yet");
            } 
            return;
          }
          var tran = MyApp.db.transaction("todostore");
          var req = tran.objectStore("todostore").get(key);
    
          req.onsuccess = function (e) {           
            success(e.target.result);
          };
        }
      };
    })();
    
    //consumer of library would ideally just do something like this:
    
    var worked = function(e) {
       //do something...
    };
    
    var didntWork = function(e) {
       //report the error, e.
    };
    
    MyApp.todolist.get(1, worked, didntWork);
    

    You should also consider offering a callback method for your client to make use of to determine when the database is ready (or not). If nothing else, at least provide some way for them to easily check if the database is ready through a method. There are many options available to you based on how you want to present the tool to your users.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to refactor some code here that was done previously by other guys,
I'm trying to clean up some old code that I wrote to comply with
I have some debugging functions that I would like to refactor, but seeing as
I have inherited some verbose, repetitious code that I am trying to refactor. The
Trying to refactor some code that has gotten really slow recently and I came
I am trying to refactor some delphi 7 code. One of the procedures I'd
I have some mutable scala code that I am trying to rewrite in a
I am trying to refactor some code in an ASP.Net website and having a
I am trying to refactor some code while leaving existing functionality in tact. I'm
I'm trying to refactor some code and move some of my before_filter's from the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.