Coffeescript variable scopes on node_redis hgetall callback function

I have the following code:

genUserLeagueDiscussionTable = (userLeagueId, firstEntry, displayEntries) ->
  # Generate League Discussion
  leagueDiscussionTable = 
    striped:   yes
    bordered:  yes
    hover:     no
    condensed: yes
    columns:   [{head:"Name"}, {head:"Comments"}]

  console.log "get userLeague #{userLeagueId}"
  LeagueId = userLeagueId

  getLeagueDiscussionEntries =  (entry, callback) =>
    getDiscussionEntry = (err,cnreply) ->
      setDiscussionTableRows = (err,replyObj,request) ->
        leagueDiscussionTable.rows[entry.dataentrynum].data = [replyObj.nickname,cnReply.entryText]
      console.log "Reply received for entry ",entry.entrynum
      console.log cnreply.entryText
      loginclient.hgetall 'uid:'+cnReply.postingUid, setDiscussionTableRows
      callback()
    lookupentry = entry.lookupentry
    console.log "getting entry for ", entry, " database key ", lookupentry
    leagueclient.hgetall lookupentry, getDiscussionEntry
  getLeagueInfo = (err,lreplyObj) =>
    console.log "got league info", lreplyObj
    leagueDiscussionTable.caption = lreplyObj.leagueName+" League Info"
    console.log "caption", leagueDiscussionTable.caption
    leagueDiscussionEntries = lreplyObj.currentDiscussionEntry
    console.log "Caption ",leagueDiscussionTable.caption," entries ", leagueDiscussionEntries, " Display Entries ",displayEntries
    lastEntry = Math.min(leagueDiscussionEntries,firstEntry+displayEntries)
    console.log "start entry ", firstEntry, " last entry ", lastEntry
    entries=[]
    for num in [firstEntry..lastEntry]
      console.log num,".a"
      entries[num]={}
      entries[num].entrynum=num
      console.log num,".b"
      entries[num].lookupentry="userLeagueId:#{LeagueID}:discussionEntryId:#{entry}"
      console.log num,".c"
      entries[num].dataentrynum = num-firstEntry
      console.log num," ", entries[num]
    console.log "Lookup the entries ", entries
    async.forEach entries, getLeagueDiscussionEntries
    , (err,reply) ->
      console.log "callback 1"
  leagueclient.hgetall "userLeagueId:#{LeagueId}", getLeagueInfo
  return leagueDiscussionTable

No matter what I do, I cannot access LeagueId or userLeagueId in getLeagueInfo. It works ok in the outside wrapper, but I can't access it inside the callback function.

What am I doing wrong?

Thanks for the help.

[Edit] Is there a way for me to pass that as a parameter to the function through the hgetall call to the callback?

Fred

Your code has many dependencies so it’s difficult to understand in isolation, but I suggest changing LeagueId to an instance variable:

`@leagueId = userLeagueId`

and then reference @leagueId instead inside getLeagueInfo.