ujjwalguptaofficial / JsStore

Simplifying IndexedDB with SQL like syntax and promises
http://jsstore.net/
MIT License
858 stars 110 forks source link

Cannot read property 'forEach' of undefined #28

Closed NJseo closed 6 years ago

NJseo commented 6 years ago

I have the following error and I cannot see where its coming from: the orgination. https://www.smartychat.co/InfluenceIO/storeAnalytics.html

jsstore.js:899 Uncaught TypeError: Cannot read property 'forEach' of undefined at new Table (jsstore.js:899) at DataBase. (jsstore.js:996) at Array.forEach () at new DataBase (jsstore.js:995) at Object. (jsstore.js:1756) at Object. (jsstore.js:657) at Object.KeyStore.processFinishedRequest (jsstore.js:97) at Object. (jsstore.js:81) at Main.returnResult (jsstore.js:377) at Main. (jsstore.js:382)

NJseo commented 6 years ago

I am trying to test the storage mechanism so the page my be still blank while you are viewing it.

ujjwalguptaofficial commented 6 years ago

You are creating jsstore instance multiple times, also creating database multiple times. I can understand this is kinda of new , so may take time.

Please have a look at - http://jsstore.net/tutorial for a good understanding. Also I have linked a js fiddle, you can use it. There is a example folder in GitHub projects - you can grab the code from there too.

Thanks

ujjwalguptaofficial commented 6 years ago

I have corrected your code. Please have a comparision with your code.

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>StoreAnalytics</title>
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <script type="text/javascript" src="https://gc.kis.v2.scr.kaspersky-labs.com/934A3130-E638-FA4A-9F28-235C14772761/main.js"
        charset="UTF-8"></script>
    <link rel="stylesheet" crossorigin="anonymous" href="https://gc.kis.v2.scr.kaspersky-labs.com/16727741C532-82F9-A4AF-836E-0313A439/abn/main.css"
    />
    <script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
        crossorigin="anonymous"></script>
    <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" />
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" />
    <script src="JsStore/Dist/Stable/jsstore.js "></script>
</head>

<body>
    <script>
        // Greeting from ujjwal : I am going to make your code work and also splitting the code for better readability.
        // please do a comparision from your code, so that you can find where you were doing mistake.

        function log(msg) {
            console.log(msg);
        }

        var db_name = "INFLUENCER_analytics";
        // this should be executed one time, not multiple time. One connection and use always.
        var Connection = new JsStore.Instance();

        window.onload = function () {
            initJsStore();
        }

        function initJsStore() {
            JsStore.isDbExist(db_name).
            then(function (isExist) {
                if (isExist) {
                    Connection.openDb(db_name);
                    log('Db already created');
                } else {
                    log('Db does not exist - creating..');
                    // va
                    // Connection.createDb(Database);
                    var DataBase = getDbSchema();
                    Connection.createDb(DataBase, function () {
                        log('Db created successfully');
                    });
                }
            });
        }

        function getDbSchema() {
            var Table1 = {
                Name: "audioID",
                Columns: [{
                        Name: "id",
                        DataType: JsStore.Data_Type.Number,
                        PrimaryKey: true,
                        AutoIncrement: false // no need to declare this field here, since by default autoincrement is false.
                    },
                    {
                        Name: "soundCard",
                        NotNull: true,
                        DataType: JsStore.Data_Type.String
                    },
                    {
                        Name: "hits",
                        DataType: JsStore.Data_Type.Number
                    }
                ]
            };

            var Table2 = {
                Name: "sessionID",
                Columns: [{
                        Name: "id",
                        DataType: JsStore.Data_Type.Number,
                        PrimaryKey: true,
                        AutoIncrement: false
                    },
                    {
                        Name: "url",
                        NotNull: true,
                        DataType: JsStore.Data_Type.String
                    },
                    {
                        Name: "start",
                        DataType: JsStore.Data_Type.Number
                    },
                    {
                        Name: "browserID",
                        NotNull: true,
                        DataType: JsStore.Data_Type.String
                    },
                    {
                        Name: "audioID",
                        NotNull: true,
                        DataType: JsStore.Data_Type.String
                    }
                ]
            };

            var Table3 = {
                Name: "pagesVisited",
                Columns: [{
                        Name: "id",
                        DataType: JsStore.Data_Type.Number,
                        PrimaryKey: true,
                        AutoIncrement: true
                    },
                    {
                        Name: "url",
                        NotNull: true,
                        DataType: JsStore.Data_Type.String
                    },
                    {
                        Name: "start",
                        DataType: JsStore.Data_Type.Number
                    },
                    {
                        Name: "touchpoints",
                        NotNull: false, // No need to declare this field here, default value of not null is false
                        DataType: JsStore.Data_Type.String
                    }
                ]
            };

            var Table4 = {
                Name: "browserID",
                Columns: [{
                        Name: "id",
                        DataType: JsStore.Data_Type.Number,
                        PrimaryKey: true,
                        AutoIncrement: false
                    },
                    {
                        Name: "name",
                        NotNull: true,
                        DataType: JsStore.Data_Type.String
                    },
                    {
                        Name: "version",
                        DataType: JsStore.Data_Type.String
                    },
                    {
                        Name: "screenSize",
                        NotNull: false,
                        DataType: JsStore.Data_Type.String
                    }
                ]
            };

            var Table5 = {
                Name: "networkID",
                Columns: [{
                        Name: "id",
                        DataType: JsStore.Data_Type.Number,
                        PrimaryKey: true,
                        AutoIncrement: false
                    },
                    {
                        Name: "name",
                        NotNull: true,
                        DataType: JsStore.Data_Type.String
                    },
                    {
                        Name: "version",
                        DataType: JsStore.Data_Type.String
                    },
                    {
                        Name: "hits",
                        NotNull: false,
                        DataType: JsStore.Data_Type.Number
                    }
                ]
            };

            var Table6 = {
                Name: "userHandles",
                Columns: [{
                        Name: "id",
                        DataType: JsStore.Data_Type.Number,
                        PrimaryKey: true,
                        AutoIncrement: false
                    },
                    {
                        Name: "Fname",
                        NotNull: true,
                        DataType: JsStore.Data_Type.String
                    },
                    {
                        Name: "Lname",
                        DataType: JsStore.Data_Type.String
                    },
                    {
                        Name: "SocialConnector",
                        NotNull: false,
                        DataType: JsStore.Data_Type.Number
                    }
                ]
            }

            var DataBase = {
                Name: db_name,
                Tables: [Table1, Table2, Table3, Table4, Table5, Table6]
            };
            return DataBase;
        }
    </script>
</body>

</html>
NJseo commented 6 years ago

Wow, I really appreciate that. Didn't think you would do the code for me but its much appreciated. Now that I got it, could you remove it from the forum?