0

I want to retrieve a user's data by his mail from MongoDB using ReactiveMongo driver for play framework but it returns: Future(<not completed>)

Here is my code:

def findBymail(email: String) = {
  val query = Json.obj("mail" -> email)
  val resul = collection.flatMap(_.find(query).one[Users])
  Logger.warn(s"result found: $res") 
}
cchantep
  • 9,118
  • 3
  • 30
  • 41
  • Actually, your `findBymail` returns `Unit`, rather than returning the `find` result, which is a `Future[Option[Users]]` (as intended for async query) – cchantep Mar 08 '18 at 10:19

1 Answers1

1

All operations in ReactiveMongo is async, it always returns Future, So you can print the result with

collection.flatMap(_.find(query).one[Users]).map{ u => Logger.warn(s"result found: $res")

I think this may not you want, you can return the Future as well, and handle the result,

def findBymail(email: String) = {
  val query = Json.obj("mail" -> email)
  collection.flatMap(_.find(query).one[Users]).map{ user =>
      Logger.warn(s"result found: $user") 
      user
  }
}

You can handle the result as:

findBymail("....").map{ user =>
    ......
}
cchantep
  • 9,118
  • 3
  • 30
  • 41
Gary Gan
  • 96
  • 4
  • thanks ,it works but i don't understand why you used the .map methode with the flatmap.I thought that .flatMap methode will handel it – mohamedali10 Mar 08 '18 at 11:26
  • collection is a Future[Collection], in here collection.flatMap will return a Future[User], – Gary Gan Mar 08 '18 at 14:05
  • because _find.... returns a Future[User], the flatMap will flat Future[Future[User]] to Future[User], that is why I can use map after flatMap – Gary Gan Mar 08 '18 at 14:07