最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How come my callback says "undefined is not a function? - Stack Overflow

programmeradmin8浏览0评论

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 pany(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 = '/'+who+'/'+where;
        console.debug('')
        //console.debug('fail')
        console.debug(url)
        console.debug(this)
        return
    }
    var self = this;

    var url = '/'+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

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 pany(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

Share Improve this question edited Aug 9, 2013 at 13:18 rink.attendant.6 46.2k64 gold badges110 silver badges157 bronze badges asked Aug 9, 2013 at 12:35 HimmatorsHimmators 15k39 gold badges136 silver badges231 bronze badges 1
  • 1 Can you simplify your code to omit irrelevant details? Chances are, you will find your problem yourself then :) – andreister Commented Aug 9, 2013 at 12:43
Add a ment  | 

2 Answers 2

Reset to default 9
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)
}

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()
}
发布评论

评论列表(0)

  1. 暂无评论