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

  • Home
  • SEARCH
  • 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 8553617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:41:36+00:00 2026-06-11T14:41:36+00:00

i am opening a DB and then doing a transaction in it. here is

  • 0

i am opening a DB and then doing a transaction in it. here is the code

var OLC = {}
// Initialising the window.IndexedDB Object
window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
window.IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange;
window.IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction;
var db = null;

OLC.indexedDB = {};
OLC.indexedDB.db = null;
OLC.indexedDB.version = null;

OLC.indexedDB.open = function(type) {
    // opening OLCMail DB
    try {
        var dbOpenRequest = window.indexedDB.open("OLCMail");
        dbOpenRequest.onsuccess = function(event){
            OLC.indexedDB.db = dbOpenRequest.result;
            db = dbOpenRequest.result; 
            OLC.indexedDB.version = db.version;
            var thisDB = db; // Need to create this variable since the variable db is assigned to other things later
            db.onversionchange = function(e){
              console.log("Version change triggered, so closing database connection", e.oldVersion, e.newVersion, thisDB);
              thisDB.close();
            };
        console.log("Database Opened", db, event);
        };

        // Creating objectstore "MailHeaders"
        dbOpenRequest.onupgradeneeded = function(e){
            console.log("Database upgrade needed");
            db = dbOpenRequest.result;
            var transaction = dbOpenRequest.transaction;

….and so on

then i have a method loadOfflineMails whose purpose it to create a transaction and get a value from DB

OLC.indexedDB.loadOfflineMails = function () {
    console.log("Opening a new transaction.");
    try {
        var db = OLC.indexedDB.db;
        console.log(OLC.indexedDB.db);
        var objectStore = db.transaction(["MailHeaders"]).objectStore("MailHeaders");
        console.log("Object Store opened", objectStore);

        var cursor = null;

….so on

I am running it as

function _init() {
    OLC.indexedDB.open();
    OLC.indexedDB.loadOfflineMails();
}

window.addEventListener("DOMContentLoaded", _init, false);

The problem that i am having is that when OLC.indexedDB.loadOfflineMails(); is called, the db property of OLC.indexedDB is showing null and hence transactionis not being completed.

But when i alert any value before the transaction statement in loadOfflineMails() the code works fine.
ex.

OLC.indexedDB.loadOfflineMails = function () {
        console.log("Opening a new transaction.");
        try {
                    alert("ANY THING");
            var db = OLC.indexedDB.db;
            console.log(OLC.indexedDB.db);
            var objectStore = db.transaction(["MailHeaders"]).objectStore("MailHeaders");
            console.log("Object Store opened", objectStore);

            var cursor = null;

I am still not able to figure out how an alert statement is causing a null db property to convert into IDBDatabase object.
I have tried console.log, delays in place of alert but nothing works, its like alert() causes a miracle.

FYI: i have done console.log(OLC.indexedDB.db) both before and after alert()
Before alert() it is null and after alert() it is a IDBDatabase Object

  • 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-11T14:41:37+00:00Added an answer on June 11, 2026 at 2:41 pm

    The OLC.indexedDB.db property is null cause the database is not yet created when you call the second function. Database opening is executed with a callback function while the rest of your code is executed sequentially. You need to modify the code so that you will execute the loadOfflineMails function at the end of dbOpenRequest.onsuccess function.

    change this code:

    function _init() {
        OLC.indexedDB.open();
        OLC.indexedDB.loadOfflineMails();
    }
    

    into this:

    function _init() {
        OLC.indexedDB.open(null,OLC.indexedDB.loadOfflineMails);
    }
    

    pass the second function as a parameter to open database function, that way it will execute at the DB is initialized.

    and you need to modify the Open Db wrapper like this:

    OLC.indexedDB.open = function(type, callback) {
        // opening OLCMail DB
        try {
            var dbOpenRequest = window.indexedDB.open("OLCMail");
            dbOpenRequest.onsuccess = function(event){
                /*your code*/
                callback();//execute the callback function after db init is complete
            }
         }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem here with this code. I'm opening a socket, then listening
Here's some quick code I am doing. (I removed some of it to make
Here is our code: <script type=text/javascript> $(document).ready(function() { $('a#installfc').click(function(e){ e.preventDefault(); window.open(http://www.site.com/ads/tr.php?src=source,'_parent'); $(this).html(Opening Download Link..
I'm opening a file and finding the line I need, but then I have
After successfully opening Pop3Folder , and retrieving messages from it, I then sometimes get
When opening a new tkinter window, I only want the user to be able
I have a script that includes opening a file from a list and then
So i was just playing with files like opening with hex editor and doing
I'm having trouble creating a directory and then opening/creating/writing into a file in the
In this code I'm creating a new window when I click the button. In

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.