Javascript: undefined is not a function

I have a ajax call that pulls back some data(json object) which then is looped thru and the SELECT statement gets records based on the data pulled back. I think this my be some type of closures issue because I get no error except only when I try to include the count (i) (on LINE 46) but if I don't add the count (i) the RunQuery function will not get the proper record.
 
The error is
Uncaught TypeError: undefined is not a function

What am I doing wrong?

thx

...
function RunQuery(sql, params, callback) {
    OpenDatabase();
    db.transaction(function (tx) { tx.executeSql(sql, params, function (tx, result) { callback(result); }, onError); });
}
...

jQuery.each(obj, function () {
                    var contentArr = jQuery.parseJSON(obj.d);

                    var count = contentArr.length;
                    for (var i = 0; i < count; i++) {

                        var s_UniqueValue = contentArr[i].s_UniqueValue;
                        var s_sqlAction = contentArr[i].s_sqlAction;
                        var s_lastUpdate = contentArr[i].s_lastUpdate;

                        var lastUpdate = moment(s_lastUpdate);

                        sql = "SELECT * FROM  [" + TableName + "] WHERE description = ? ORDER BY lastUpdate DESC LIMIT 1";
                        params = [s_UniqueValue];

                        RunQuery(sql, params, function (ds) {

                            var rcount = ds.rows.length;

                            switch (s_sqlAction.toLowerCase()) {
                                case "insert":
                                case "update":
                                    if (rcount == 0) {

                                    } else {
                                        // Check Remote table and get the lastest (for inserts and deletes)
                                        var RemoteLastUpdated = moment(ds.rows.item(0)['lastUpdate']);
                                        if (lastUpdate > RemoteLastUpdated) {
                                            // Insert to Remote Table
                                            var rs = SyncRemote(obj.d, TableName, s_sqlAction, i);
                                        }
                                    }
                                    break;
                                case "delete":

                                    break;
                            }

                        })(i);
                    }
                });

Open in new window

quest_capitalAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GaryCommented:
What is the point of the i here, it does nothing and is not valid.
})(i);

The i is already set in the loop
0
quest_capitalAuthor Commented:
It's aclosure thing... If don't have it (i) will not be included.
0
GaryCommented:
It's not a closure on anything - its completely separate
Is it supposed to be part of the RunQuery ? Even tho that only accepts 3 params
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

quest_capitalAuthor Commented:
I needed to wrap every thing inside the for loop.
Lines 5 - 39;
See below:
var contentArr = jQuery.parseJSON(obj.d);

                    var count = contentArr.length;
                    for (var i = 0; i < count; i++) {
                        (function (i, contentArr) {

                        var s_UniqueValue = contentArr[i].s_UniqueValue;
                        var s_sqlAction = contentArr[i].s_sqlAction;
                        var s_lastUpdate = contentArr[i].s_lastUpdate;

                        var lastUpdate = moment(s_lastUpdate);

                        sql = "SELECT * FROM  [" + TableName + "] WHERE description = ? ORDER BY lastUpdate DESC LIMIT 1";
                        params = [s_UniqueValue];

                        RunQuery(sql, params, function (ds) {

                            var rcount = ds.rows.length;

                            switch (s_sqlAction.toLowerCase()) {
                                case "insert":
                                case "update":
                                    if (rcount == 0) {

                                    } else {
                                        // Check Remote table and get the lastest (for inserts and deletes)
                                        var RemoteLastUpdated = moment(ds.rows.item(0)['lastUpdate']);
                                        if (lastUpdate > RemoteLastUpdated) {
                                            // Insert to Remote Table
                                            var rs = SyncRemote(obj.d, TableName, s_sqlAction, i);
                                        }
                                    }
                                    break;
                                case "delete":

                                    break;
                            }
                        });
                    })(i, contentArr);
                    }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Sar1973Commented:
Does the RunQuery function need the syntax RunQuery(sql, params, function (ds) { ....})(other arguments);?
0
quest_capitalAuthor Commented:
...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.