我有以下代码:

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` 

然后在getLeagueInfo内引用@leagueId


评论关闭
IT源码网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!