scala - Play chain futures -


i developping small web page on play scala framework , use mongodb

member document has meberinfo include member id, , attendance document has

someone's attendance information.

so , want find members in group first , after

find member's attendance information..

my code below...

def attendancebysubgroup(group: string, sub_group: string) = action.async{      var  obj : jsobject = json.obj("group" -> group)     if(sub_group ne "all"){       obj = obj + ("sub_group" -> jsstring(sub_group))     }      val cursor: cursor[jsobject] = member_list.find(obj).cursor[jsobject]      // gather jsobjects in list     val futurememberlist: future[list[jsobject]] = cursor.collect[list]()         val futurepersonsjsonarray: future[jsarray] = futurememberlist.map {       val m = arraybuffer.empty[jsobject]       members => members.map{         member => {           val futureitem = attendance_list.find(json.obj("member_id" -> member.\("_id").\("$oid"))).one[jsobject]           futureitem.map {             case some(item) => member.+(("attendance" , item.\("attendance")))             case none => member.++(json.obj("attendance"-> false))           }         }       }       json.arr(m.tolist)     }        futurepersonsjsonarray.map {       members =>         ok(members(0))     } 

but, it's not work want to

maybe, futureitem.map { .. async call...

how can return member list include attendance information..

thanks help

i not sure , best solution...

but work correctly..

anybody find better solution..please post answer

  def attendancebysubgroup(group: string, sub_group: string) = action.async{      var  obj : jsobject = json.obj("group" -> group)     if(sub_group ne "all"){       obj = obj + ("sub_group" -> jsstring(sub_group))     }      val cursor: cursor[jsobject] = member_list.find(obj).cursor[jsobject]      // gather jsobjects in list     val futurememberlist: future[list[jsobject]] = cursor.collect[list]()      val futurememberlistwithattendance = futurememberlist.map {       members =>  members.map{         member => {            val futureitem = attendance_list.find(json.obj("member_id" -> member.\("_id").\("$oid"))).one[jsobject]            futureitem.map[jsobject] {             case some(item) => member.+(("attendance" , item.\("attendance")))             case none => member.++(json.obj("attendance"-> false))           }         }       }     }      futurememberlistwithattendance.map{      val memberattendances = arraybuffer.empty[jsobject]      aa => await.result(future.reduce(aa)((s1, s2) => {memberattendances+=s1; s1 } ) , duration.inf)      ok(json.arr(memberattendances))    }   } 

Comments