Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to call the method clean from getUser, but it returns undefined. If I call u.test(), it works perfectly.

How can I solve this issue?

class User
    constructor: () ->
        @db = # connect to db...

    clean: (user, callback) ->
        delete user.password
        callback user


   getUser: (id) ->
       @db.get id, (err, user) ->
            @clean user, (u) -> console.log u

   test: () ->
           @clean {name: "test", password: "hello"}, (u) ->
                console.log u

u = new User
u.getUser()
share|improve this question

1 Answer 1

up vote 1 down vote accepted

You want => for the inner function.

In your inner function, with ->, it's a normal function bound to undefined by default. With =>, you bind it to the this value of the function instantiation context.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.