Search code examples
mongodbscalaplayframeworkreactivemongotwirl

Play! Framework - return multiple list to template


I have 3 classes and I wanna return 3 lists to template which use in Form my db is 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))
   }
  }

template code :

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

I have got an error : Type mismatch expect List[Teacher] , actual Future[List[Teacher]]

What am I gonna do?

note : if i put Ok(views...) to val sam map, compiler show an error,it's sounds like async error becouse "async" will be red

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

Solution

  • I have fixed this problem with flatMap

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