ReactiveMongo查询返回无

时间:2016-07-20 13:00:00

标签: scala reactivemongo

我刚接触学习Scala和相关技术。我遇到的问题是loadUser应该返回一条记录,但它会变空。

我收到以下错误: java.util.NoSuchElementException:None.get

我很欣赏这不是理想的Scala,所以请随时向我推荐改进。

class MongoDataAccess extends Actor {
  val message = "Hello message"

  override def receive: Receive = {
    case data: Payload => {
      val user: Future[Option[User]] = MongoDataAccess.loadUser(data.deviceId)
      val twillioApiAccess = context.actorOf(Props[TwillioApiAccess], "TwillioApiAccess")
      user onComplete {
        case Failure(exception) => println(exception)
        case p: Try[Option[User]] => p match {
          case Failure(exception) => println(exception)
          case u: Try[Option[User]] => twillioApiAccess ! Action(data, u.get.get.phoneNumber, message)
        }
      }
    }

    case _ => println("received unknown message")
  }
}

object MongoDataAccess extends MongoDataApi {
  def connect(): Future[DefaultDB] = {
    // gets an instance of the driver
    val driver = new MongoDriver
    val connection = driver.connection(List("192.168.99.100:32768"))

    // Gets a reference to the database "sensor"
    connection.database("sensor")
  }

  def props = Props(new MongoDataAccess)

  def loadUser(deviceId: UUID): Future[Option[User]] = {
    println(s"Loading user from the database with device id: $deviceId")
    val query = BSONDocument("deviceId" -> deviceId.toString)

    // By default, you get a Future[BSONCollection].
    val collection: Future[BSONCollection] = connect().map(_.collection("profile"))

    collection flatMap { x => x.find(query).one[User] }
  }
}

由于

1 个答案:

答案 0 :(得分:0)

没有保证find-one(.one[T])与您的数据库中的至少一个文档匹配,因此您获得了Option[T]

然后你可以考虑(或不考虑)找不到文件是否失败(或不是); e.g。

val u: Future[User] = x.find(query).one[User].flatMap[User] {
  case Some(matchingUser) => Future.successful(matchingUser)
  case _ => Future.failed(new MySemanticException("No matching user found"))
}
  无论如何,在.get上使用Option是一个坏主意。