0

I am using Play2 framework with play-reactivemongo and casbah. I have the following Code:

def geofence(lat:Float, lon:Float, rad:Float) = Action { request =>
  val mongoColl = MongoConnection()("db")("geofence")
  mongoColl.ensureIndex(MongoDBObject("loc" -> "2dsphere"))

  val geo = MongoDBObject(
   "loc" -> MongoDBObject(
    "$nearSphere" -> MongoDBObject(
     "type" -> "Point",
      "coordinates" -> MongoDBList( 52.431929,
       13.506188)
    ),
   "$maxDistance" -> 10000 ))

  val geoEvents=mongoColl.find(geo)
  println(geoEvents)

  Ok(geoEvents)
}

I send a http request to the server. Depends on the parameter I would like to find all datas in my database. The problem is with mongoColl.find I get only the MongoCursor and I get an error by Ok(geoEvents)

Cannot write an instance of Option[mongoColl.T] to HTTP response. Try to define a Writeable[Option[mongoColl.T]]

Q:How can I convert the val geoEvents to json for response?

Thanks

ruby4newb
  • 11
  • 6
  • Have a look at this http://stackoverflow.com/questions/11987071/how-to-convert-casbah-mongodb-list-to-json-in-scala-play . – ccheneson Mar 03 '15 at 16:09
  • Ok(geoEvents).as("application/json") doesn't work for me! I get the same error. – ruby4newb Mar 03 '15 at 16:19
  • The error is: Try to define a Writeable[mongoColl.CursorType]. What does it mean? – ruby4newb Mar 03 '15 at 16:20
  • Have you turned your cursor into a list ? Try this `val res = geoEvents.toList.mkString(",") ; val json = Json.toJson(res) ; Ok(json).as("application/json")` – ccheneson Mar 03 '15 at 16:23

0 Answers0