Question

I am trying to create a data management application, but instead of a Windows-based solution or using WebSQL, i am using IndexedDB. I am pretty new to it but I believe I have covered the basis in this draft of code.

Anyway, my problem is, anytime I run the code, my openDB() function and the addeventListener() function both run and show on the console log at runtime but all other functions are said to be undefined when I try to run the code. What could the problem be?

In the HTML file, the jQuery script file is referenced.

       (function () {
        var DB_NAME = 'shodex';
        var DB_VERSION = 1;
        var DB_STORE_NAME = 'visitors';
        var db;

        var current_view_pub_key;

        //opens the IndexedDB database   
        function openDb() {
        console.log("open Database......");
        var req = indexedDB.open(DB_NAME, DB_VERSION);
        req.onsuccess = function (evt) {
        db = this.result;
        console.log("Database Opened");
        };
        req.onerror = function (evt) {
        console.error("openDb:", evt.target.errorCode);
        };
        req.onupgradeneeded = function (evt) {
        console.log("Event fired when DB is needed to be upgraded");
        var store = evt.currentTarget.result.createObjectStore(
        DB_STORE_NAME, { keyPath: 'id', autoIncrement: true });
        store.createIndex('name', 'name', { unique: false });
        store.createIndex('date', 'date', { unique: false });
        store.createIndex('whom_to_see', 'whom_to_see', { unique: false });
        store.createIndex('arrival_time', 'arrival_time', { unique: false });
        store.createIndex('reason', 'reason', { unique: false });
        store.createIndex('departure_time', 'departure_time', { unique: false });
        };
          }

        //used to create a transaction
        function getObjectStore(store_name, mode) {
        var tx = db.transaction(store_name, mode);
        return tx.objectStore(store_name);
        }

        //adds a Visitor to the IndexedDB
        function addVisitor(name, date, to_see, arrival_time, reason, departure_time) {
        console.log("Adding the following data to IndexedDB: ", arguments);
        var obj = { name: name, date: date, whom_to_see: to_see, arrival_time: arrival_time, reason: reason, departure_time: departure_time };
        if(typeof blob != undefined)
        {
            obj.blob = blob;
        }
        var store = getObjectStore(DB_STORE_NAME, 'readwrite');
        var req;
        try
        {
            req = store.add(obj);
        }
        catch(e)
        {
            if(e.name == 'DataCloneError')
            displayActionFailure("This engine does not know how to clone a Blob, use Firefox!");
            throw(e);
        }
        req.onsuccess = function (evt) {
            console.log("Insertion into DB was successful. You can heave a huge sigh of relief!");
            displayActionSuccess();
        };
        req.onerror = function () {
            console.error("Insertion into DB failed!");
            displayActionFailure(this.error);
        };
    }

    function displayActionSuccess() {
        alert("Whatever the heck you were doing was successful. Congrats!");
    }

    function displayActionFailure() {
        alert("Oh Oh! System Failure! System Failure!");
    }

     // listens for the submit button event  
    function addEventListeners() {
        console.log("Event Listeners");
        $('#addVisitor').click(function(evt) {
            console.log("Add Visitors Submit button");
            var name = document.getElementsByName("txtName").value;
            var date = document.getElementsByName("txtDate").value;
            var whom_to_see = document.getElementsByName("txtToSee").value;
            var time_of_arrival = document.getElementsByName("txtArrivalTime").value;
            var reason_for_visit = document.getElementsByName("txtReason").value;
            var time_of_departure = document.getElementsByName("timeOfDep");
            addVisitor(name, date, whom_to_see, time_of_arrival, reason_for_visit, time_of_departure);
        });
    }
//makes the database open at runtime
openDb();
addEventListeners();
})
();
Was it helpful?

Solution 3

Instead of referencing the javascript code like before...

I removed all other functions from the javascript file leaving the openDB() function and placed them in the main html file. it worked automatically.

OTHER TIPS

syntax error - you need a closing round bracket at end. i.e add

);

I think the problem is the fact that when your anonymous function is run the browser doesn't not know about the '#addVisitor' element, so the click event handler for that element is not created.

You should put your code inside "$(function() {" or "$(document).ready(function() {":

$(function() {
    (function () {
        var DB_NAME = 'shodex';
        var DB_VERSION = 1;
        ...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top