user1952811
user1952811

Reputation: 2468

Calling functions in an async function javascript (Meteor) Getting undefined

The issue seems to be that calling the function vote_ipcheck() and vote_cookie_check() both are throwing an error Uncaught TypeError: undefined is not a function. If I put the contents of the function inside the $.getJSON call then it's not an issue, however calling the function throws that error.

If anyone's got an idea as to why something like this is occurring, it would be great.

    if (ip_check) {
        $.getJSON("http://smart-ip.net/geoip-json?callback=?", function(data){

            console.log(data.host);

            var vote_ipcheck = vote_ipcheck(data.host);
            var vote_cookie_check = vote_cookie_check();

            if (vote_ipcheck && vote_cookie_check) {
                Router.go('pollyResults', {_id: id});
            } else if (vote_ipcheck == false && vote_cookie_check == false) {
                update_poll();
            }

        });
    }

    function vote_cookie_check() {
      // Handling the cookie business
      console.log(ReactiveCookie.list());

      if (ReactiveCookie.get('voted')) {
        var object_voted = JSON.parse(ReactiveCookie.get('voted'));
        if (id in object_voted) {
            if (object_voted[id] == true) {
                    return true;
            }
        } else {
            object_voted[id] = true;
            ReactiveCookie.set('voted', JSON.stringify(object_voted), {days: 365});
            return false;
        }
      } else {
            var object_voted = {};
            object_voted[id] = true;
            ReactiveCookie.set('voted', JSON.stringify(object_voted), {days: 365});
            return false;
      }
    }

    function vote_ipcheck(ip) {
        ip_voted = ip_array.indexOf(ip);

        if (ip_voted > -1) {
            return true;
        }
        else {
            Polls.update({_id: id}, {$push : {already_voted : ip}});
            return false;           
        }
    }

Upvotes: 0

Views: 109

Answers (1)

Igor
Igor

Reputation: 15893

Do not redefine vote_ipcheck and vote_cookie_check in the local scope if you want to use global functions with these names. Give the local variables different names.

        var ipcheck = vote_ipcheck(data.host);
        var cookie_check = vote_cookie_check();

        if (ipcheck && cookie_check) {
            Router.go('pollyResults', {_id: id});
        } else if (ipcheck == false && cookie_check == false) {
            update_poll();
        }

Upvotes: 1

Related Questions