2013-08-09 12 views
6

sto chiamando una funzione con un callback come questo:?Come mai il mio callback dice "indefinito non è una funzione

$(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') 
      }); 
     }) 
    }); 
}); 

E 'l'ultimo richiamata con console.log ('test'), che è il . problema

questo è il getPhone funzione:

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() 
      } 
     } 
    }) 
} 

la richiamata viene attivato quando si suppone di Ma quando il callback è presente ottengo l'errore:.

undefined is not a function

+1

Riesci a semplificare il codice di omettere dettagli irrilevanti? Le probabilità sono, troverai il tuo problema te allora :) – andreister

risposta

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

Quando fai il tentativo successivo, non stai passando sul callback - allora non è definito in quella chiamata. Uno dovrebbe sempre verificare se un callback è una funzione prima di richiamarlo.

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) 
} 
6

Non sono sicuro se questo è un problema, ma è un'idea di partenza:

Nella tua ultima riga avete self.getPhone(), dove non si passa una richiamata. Quindi, se si raggiunge questo codice: callback(); nel metodo getPhone, callback potrebbe non essere definito.

Prova:

if (typeof(callback) === 'function') { 
    callback() 
} 
Problemi correlati