玩!框架 - 将多个列表返回到模板

时间:2015-02-04 07:43:55

标签: mongodb scala playframework reactivemongo twirl

我有3个课程,我想将3个列表返回到Form中使用的模板 我的数据库是MongoDB

def addCourse = Action.async {implicit request =>
  val teacherCollection = db.collection[BSONCollection]("Teacher")
   val courseColl = subjectCollection.find(BSONDocument()).cursor[Subject].collect[List]()
   val teacherColl = teacherCollection.find(BSONDocument()).cursor[Teacher].collect[List]()

    courseColl.map { course =>
      val sam = teacherColl.map{teacher=>
        teacher
      }
    Ok(views.html.Course.addNewCourse(course,sam,Course.form))
   }
  }

模板代码:

   @(subject:List[models.Subject],teacher:List[models.Teacher],myForm: Form[models.Course])

我有一个错误:类型不匹配期望列表[老师],实际未来[列表[老师]]

我该怎么办?

注意:如果我把Ok(views ...)放到val sam map中,编译器显示错误,它听起来像异步错误因为#as;" async"将是红色

 Error:(59, -1) Play 2 Compiler: 
 /app/controllers/School.scala:59: type mismatch;
found   : Unit
  required: play.api.mvc.Result

1 个答案:

答案 0 :(得分:0)

我用flatMap修复了这个问题

courseColl.flatMap { course =>
  teacherColl.flatMap { teacher =>
    semesterColl.map { semester =>
      Ok(views.html.Course.addNewCourse(course, teacher, semester,    Course.form))
    }
  }
}