sql >> Base de Datos >  >> NoSQL >> MongoDB

Mi PlayFramework Action regresa antes de que un futuro esté listo, ¿cómo actualizo un componente de página web?

Action no está diseñado para trabajar con futuros. Usa Action.async , que "esperará" (técnicamente no esperará, pero programará) a que termine el futuro:

def rect(swLon: Float, swLat: Float, neLon: Float, neLat: Float) = Action.async {
  val sb = new StringBuilder()
  sb.append("<tt>boundingBox: swLon=" + swLon + ", swLat=" + swLat + ", neLon=" + neLon + ", neLat=" + neLat + "</tt>")
  if (oDb.isDefined) {
    val collection: MongoCollection[Document] = oDb.get.getCollection(collectionName)
    val fut = getFutureOne(collection) // returns a Future[Seq[Document]]
    fut.map {docs => 
      setMongoJson(doc.toJson)
      Ok(sb.toString)
    } recover {
      case e => BadRequest("FAIL: " + e.getMessage)
    }
  } else Future.successful(Ok("Not defined"))
}

Eche un vistazo a esto como referencia:https://www.playframework.com/documentation /2.4.x/ScalaAsync