1

I am using slick 3.1.1 and haven't been able to figure out how I can convert a query in Json. I have this simple query that returns a fullname

def listfollowing() = Action {

    val setup = DBIO.seq(
      sql"""select p.fullname from profiles p join relations r on(p.id=r.following) where r.me=32;""".as[(String)]
    )
    val setupFuture = db.run(setup)


    Ok(Json.toJson(setupFuture.toString))


  }

Which simply gives of this result

"scala.concurrent.impl.Promise$KeptPromise@6a3903"

Any suggestions would be great

Here some additional information:

import play.api.data.Form
import play.api.libs.json.{Json, JsValue}
import play.api.mvc.{Action, Controller}
import slick.dbio.DBIO
import slick.driver.PostgresDriver.api._
import play.api.data.Forms._
import scala.concurrent.ExecutionContext.Implicits.global

  val db= Database.forConfig("database")
  try {
    // ...
  } finally db.close

I connect to slick as follows

database {
  dataSourceClass = "org.postgresql.ds.PGSimpleDataSource"
  properties = {
    databaseName = "mydbname"
    user = "user"
    password = "password"
  }
  numThreads = 10

}

1 Answer 1

1

setupFuture in your case is a Future, which means it (may) hold a value in the future. To get "inside" a Future, you will want to .map on it (if you're more familiar with the frontend world, this is similar to Javascript Promises / .then). The body of the map will run when the future's value is available.

Here's an implementation that will return real data:

def listfollowing() = Action.async {
  val setup = DBIO.seq(
    sql"""select p.fullname from profiles p join relations r on(p.id=r.following) where r.me=32;""".as[String]
  )
  db.run(setup).map { result =>
    Ok(Json.toJson(result.toString))
  }
}

Take special note of Action.async.

1
  • Oh ok I just ran it and I have a few questions. If I put in the code just like that I get an error on the db.run(setup) which is: Cannot find an implicit ExecutionContext. You might pass an (implicit ec: ExecutionContext) parameter to your method or import scala.concurrent.ExecutionContext.Implicits.global. Then i import scala.concurrent.ExecutionContext.Implicits.global and it compiles but I get another error: IllegalStateException: Cannot initialize ExecutionContext; AsyncExecutor already shut down . I will post my setup above Feb 3, 2016 at 20:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.