我有以下代码:

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


无论我做什么,都无法访问getLeagueInfo中的LeagueId或userLeagueId。在外部包装器中它可以正常工作,但是我无法在回调函数中访问它。

我究竟做错了什么?

谢谢您的帮助。

[编辑]我有办法通过对回调的hgetall调用将其作为参数传递给函数吗?

弗雷德

最佳答案

您的代码具有很多依赖关系,因此很难孤立地理解它,但是我建议将LeagueId更改为实例变量:

`@leagueId = userLeagueId`


然后在@leagueId内引用getLeagueInfo

关于node.js - node_redis hgetall回调函数上的Coffeescript变量范围,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13152517/

10-09 21:23