Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How come my callback says "undefined is not a function?

I'm calling a function with a callback like this:

$(function() {
    //get all the items
    search.init('.result tbody tr');
    search.parseresults(function(announcementID){
        //query every single page
        var myCompany = new company(announcementID);
        myCompany.requestPage(function(){
            //on response parse the data.
            myCompany.parsedata()
            var myPerson = new person(myCompany )
            myPerson.getPhone(function(){
                console.log('test')
            });
        })
    });
});

It's the last callback with console.log('test') that is the problem.

this is the getPhone-function:

person.prototype.getPhone = function(callback){
    this.attempt++
    if( this.attempt === 1){
        var who = this.lastname;
        var where = this.adress+' '+this.postal;
    }else if(this.attempt === 2){
        var who = this.firstname+' '+this.lastname;
        var where = this.adress+' '+this.postal;
    }else{
        var who = this.firstname+' '+this.lastname;
        var where = this.adress+' '+this.postal;
        var url = 'http://personer.eniro.se/resultat/'+who+'/'+where;
        console.debug('')
        //console.debug('fail')
        console.debug(url)
        console.debug(this)
        return
    }
    var self = this;

    var url = 'http://personer.eniro.se/resultat/'+who+'/'+where;
    GM_xmlhttpRequest({
        method: "GET",
        url: url,
        onload: function(data) {
            data = $.parseHTML(data.response);
            var vCard = $(data).find('.vcard')
            if (vCard.length === 1){
                var phone = vCard.find('.tel.row a').map(function(){
                    return this.text
                }).get()

                self.officePhone = phone[0];
                if(phone.length > 1){
                    self.mobilePhone = phone[1];
                }else{
                    self.mobilePhone = '';
                }
                callback();

            } else if(vCard.length > 1){
                self.getPhone()
            }
        }
    })
}

The callback gets triggered when it's supposed to. But when the callback is present I get the error:

undefined is not a function

like image 287
Himmators Avatar asked Aug 09 '13 12:08

Himmators


People also ask

Why is callback undefined in getphone method?

In your last line you have self.getPhone (), where you do not pass a callback. So, if you reach this code: callback (); in the getPhone method, callback may be undefined. Thanks for contributing an answer to Stack Overflow!

How do I fix the callback is not a function error?

The "callback is not a function" error occurs when we define a callback parameter to a function, but invoke the function without passing a callback as a parameter. To solve the error, provide a function as a default parameter, or always provide a parameter when calling the function. Here is an example of how the error occurs.

Why is my callback not passing to next attempt?

When you're doing the next attempt, you're not passing on the callback - then is undefined in that call. One should always test whether a callback is a function before invoking it.

Why does the function callback throw an exception when calling it?

However, when you called the function you only gave it ONE parameter: input Hence, when your code is executed and it reaches this part of the function var key = callback (item); it throws and exception because callback is undefined since you did not pass in a second argument.


2 Answers

Not sure if this is a problem but it's a starting idea:

In your last line you have self.getPhone(), where you do not pass a callback. So, if you reach this code: callback(); in the getPhone method, callback may be undefined.

Try:

if (typeof(callback) === 'function') {
    callback()
}
like image 43
Dennis Avatar answered Nov 14 '22 22:11

Dennis


else if(vCard.length > 1){
    self.getPhone()
}

When you're doing the next attempt, you're not passing on the callback - then is undefined in that call. One should always test whether a callback is a function before invoking it.

if (vCard.length === 1){
    var phone = vCard.find('.tel.row a').map(function(){
        return this.text
    }).get()

    self.officePhone = phone[0];
    if(phone.length > 1){
        self.mobilePhone = phone[1];
    }else{
        self.mobilePhone = '';
    }
    // also pass some reasonable result:
    if (typeof callback=="function") callback(phone);

} else if(vCard.length > 1) {
    self.getPhone(callback)
}
like image 174
Bergi Avatar answered Nov 14 '22 22:11

Bergi