玩!框架 - return 多个列表到模板
Play! Framework - return multiple list to template
我有 3 个 类,我想要 return 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])
我遇到一个错误:类型不匹配,预期为 List[Teacher],实际为 Future[List[Teacher]]
我要做什么?
注意:如果我将 Ok(views...) 放入 val sam 映射,编译器会显示错误,这听起来像是异步错误,因为 "async" 将是红色的
Error:(59, -1) Play 2 Compiler:
/app/controllers/School.scala:59: type mismatch;
found : Unit
required: play.api.mvc.Result
我已经用 flatMap 解决了这个问题
courseColl.flatMap { course =>
teacherColl.flatMap { teacher =>
semesterColl.map { semester =>
Ok(views.html.Course.addNewCourse(course, teacher, semester, Course.form))
}
}
}
我有 3 个 类,我想要 return 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])
我遇到一个错误:类型不匹配,预期为 List[Teacher],实际为 Future[List[Teacher]]
我要做什么?
注意:如果我将 Ok(views...) 放入 val sam 映射,编译器会显示错误,这听起来像是异步错误,因为 "async" 将是红色的
Error:(59, -1) Play 2 Compiler:
/app/controllers/School.scala:59: type mismatch;
found : Unit
required: play.api.mvc.Result
我已经用 flatMap 解决了这个问题
courseColl.flatMap { course =>
teacherColl.flatMap { teacher =>
semesterColl.map { semester =>
Ok(views.html.Course.addNewCourse(course, teacher, semester, Course.form))
}
}
}