0

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 ?

5
  • stackoverflow.com/questions/28622467/… This will help! Commented Oct 1, 2015 at 6:55
  • @SandeepNayak nope, I try it :( Commented Oct 1, 2015 at 7:00
  • 1
    It would appear something is changing the context of your object. Either when you require it. Or something before when you call it. Console.log this inside it to see if the context is what you expect. You can also bind it, or use call etc to set it but you should find whats changing it. Also why are you aliasing this with self? There's no need. Also you setting number mysql and validator twice. You should either pass it into searching for it to set or set it in test and just read them in searching. Commented Oct 1, 2015 at 7:02
  • 1
    Your error is referring to a line of code that isn't present in your question. While it is likely this is because you're executing test in such a way that this does not point to the correct object, it could be that your error is from a different section of code. Commented Oct 1, 2015 at 7:05
  • 1
    See also: unicodegirl.com/this-is-the-calling-object.html Commented Oct 1, 2015 at 7:21

1 Answer 1

1

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); } 
Sign up to request clarification or add additional context in comments.

1 Comment

How can I included searching function to this scope ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.