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()
Arjun Bajaj
  • 1,932
  • 7
  • 24
  • 41

1 Answers1

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.

Havvy
  • 1,471
  • 14
  • 27