0
私は、Scalaや関連する技術を学ぶのが初めてです。私は、loadUserがレコードを返すべきであるが、空になるという問題を抱えています。ReactiveMongoクエリが返されません。
私は、次のエラーを取得しています: java.util.NoSuchElementException:None.get
私はこれに感謝は理想的なスカラ座ではないので、私の改善を示唆して自由に感じます。
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] }
}
}
おかげ
[オプション[ユーザー]]それがかもしれないか、一度ユーザーを取得していない可能性があるとして、呼び出し元のコードを欺いている[ユーザー]未来が完了しました。とにかく、提案した変更はまだ返されていません。 –
'Future(Option.empty [T])'が返された場合、単に一致する文書がないことを意味します。 – cchantep
MongoDB query db.getCollection( 'profile')。find({"deviceId": "03af30d0-4dc6-11e6-beb8-9e71128cae77"})は正しい文書を返します。 –