Calling function from an object instance not working
Problem
I have this code here:
var myRequest = new httpReq.myRequest(buffer,socket);
if(!myRequest.hasSucceded()){ //return error
return;
}
arr=myRequest.getCookies();
....
and i definitely have this Function on my myRequest object:
function myRequest(buffer, socket) {
...
this.cookies = new Array();
...
//returns the cookie array
function getCookies() {
return this.cookies;
}
...
}
exports.myRequest = myRequest;
and i get an error saying:
TypeError: Object # has no method 'getCookies'
Y U NO GIVE COOKIES???
help please...
Problem courtesy of: Itzik984
Solution
You are declaring getCookies as a local function.
To call it externally, you need to create a property getCookies on your object so you can call it.
Try this:
function myRequest(buffer, socket) {
...
this.cookies = new Array();
...
//returns the cookie array
this.getCookies = function() {
return this.cookies;
}
...
}
You also could just do myRequest.cookies
instead of myRequest.getCookies()
Solution courtesy of: Alan Geleynse
Discussion
View additional discussion.