Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Calling function from an object instance not working

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.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Calling function from an object instance not working

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×