4

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()

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.