Alyreza
Alyreza

Reputation: 91

NodeJs undefined is not a function

I'm using NodeJs to run this code this is my custom module

call = {};
call.hangup = {
    searching: function(number, mysql, validator){
        this.number = number;
        this.mysql = mysql;
        this.validator = validator;
        var query = "{sql...}";
        try
        {   
            mysql.query(query, function(err, rows, fields) {        
            if (err) throw err; 
                if(!validator.isNull(rows))
                {                                       
                    return rows.leadid;                     
                }else {
                    return false;
                }
            });
        }catch(error)
        {
            console.log(error);
        }
    },
    test: function(number, mysql, validator){
        var self = this;
        this.number = number;
        this.mysql = mysql;
        this.validator = validator;
        var result = self.searching(number, mysql, validator);
        console.log(result);
    }
};
module.exports = call;

then call test function in my main file

call.hangup.test(number, connection, validator);

but I'm getting this error code in my console :

                var result = self.searching(leadid, mysql, validator);
                                  ^
TypeError: undefined is not a function

how can I fix it ? and why this happen ?

Upvotes: 0

Views: 289

Answers (1)

semirturgay
semirturgay

Reputation: 4201

Your this reference (which you assigned to self variable)points to test function and test function does not have a function called searching.That is why you got this error. You should call searching function in this way call.hangup.searching

call = {};
call.hangup = {
    searching: function(number, mysql, validator){
        this.number = number;
        this.mysql = mysql;
        this.validator = validator;
        var query = "{sql...}";
        try
        {   
            mysql.query(query, function(err, rows, fields) {        
            if (err) throw err; 
                if(!validator.isNull(rows))
                {                                       
                    return rows.leadid;                     
                }else {
                    return false;
                }
            });
        }catch(error)
        {
            console.log(error);
        }
    },

    test: function(number, mysql, validator){

        this.number = number;
        this.mysql = mysql;
        this.validator = validator;
        var result = call.hangup.searching(number, mysql, validator);
        console.log(result);
    }
};
module.exports = call;

EDIT: You can also assigne self variable to call.hangup

test: function(number, mysql, validator){
            var self = call.hangup;
            this.number = number;
            this.mysql = mysql;
            this.validator = validator;
            var result = self.searching(number, mysql, validator);
            console.log(result);
        }

Upvotes: 1

Related Questions