与备选方案异步的重载方法值:
overloaded method value async with alternatives:
当我有以下代码时,一切都可以正常编译。
def create(name: String, age: Int) = Action.async {
val json = Json.obj(
"name" -> name,
"age" -> age,
"created" -> new java.util.Date().getTime())
collection.insert(json).map(lastError =>
Ok("Mongo LastError: %s".format(lastError)))
}
但是提取内容到另一个方法时如下
def create(name: String, age: Int) = Action.async {
createPerson(name,age)
}
def createPerson(name: String, age: Int) = Action.async {
val json = Json.obj(
"name" -> name,
"age" -> age,
"created" -> new java.util.Date().getTime())
collection.insert(json).map(lastError =>
Ok("Mongo LastError: %s".format(lastError)))
}
它给出了错误
[error] /media/pubudu/NTFSPartition/projects/catnet/data-collector-backend/app/controllers/Application.scala:50: overloaded method value async with alternatives:
[error] [A](bodyParser: play.api.mvc.BodyParser[A])(block: play.api.mvc.Request[A] => scala.concurrent.Future[play.api.mvc.Result])play.api.mvc.Action[A] <and>
[error] (block: play.api.mvc.Request[play.api.mvc.AnyContent] => scala.concurrent.Future[play.api.mvc.Result])play.api.mvc.Action[play.api.mvc.AnyContent] <and>
[error] (block: => scala.concurrent.Future[play.api.mvc.Result])play.api.mvc.Action[play.api.mvc.AnyContent]
[error] cannot be applied to (play.api.mvc.Action[play.api.mvc.AnyContent])
[error] def create(name: String, age: Int) = Action.async {
这两个代码段不是在做同样的事情吗?如果没有,我如何提取方法的内容而不会出现错误?我想做的是从两个不同的地方调用提取的方法。
create
方法期望return类型是Future[Result]
,但是createPerson
return的调用是Action[AnyContent]
。您可以通过进行以下更改来更正它:
def create(name: String, age: Int) = Action.async {
createPerson(name,age).map(lastError =>
Ok("Mongo LastError: %s".format(lastError)))
}
def createPerson(name: String, age: Int):Future[String] = {
val json = Json.obj(
"name" -> name,
"age" -> age,
"created" -> new java.util.Date().getTime())
collection.insert(json)
}
当我有以下代码时,一切都可以正常编译。
def create(name: String, age: Int) = Action.async {
val json = Json.obj(
"name" -> name,
"age" -> age,
"created" -> new java.util.Date().getTime())
collection.insert(json).map(lastError =>
Ok("Mongo LastError: %s".format(lastError)))
}
但是提取内容到另一个方法时如下
def create(name: String, age: Int) = Action.async {
createPerson(name,age)
}
def createPerson(name: String, age: Int) = Action.async {
val json = Json.obj(
"name" -> name,
"age" -> age,
"created" -> new java.util.Date().getTime())
collection.insert(json).map(lastError =>
Ok("Mongo LastError: %s".format(lastError)))
}
它给出了错误
[error] /media/pubudu/NTFSPartition/projects/catnet/data-collector-backend/app/controllers/Application.scala:50: overloaded method value async with alternatives:
[error] [A](bodyParser: play.api.mvc.BodyParser[A])(block: play.api.mvc.Request[A] => scala.concurrent.Future[play.api.mvc.Result])play.api.mvc.Action[A] <and>
[error] (block: play.api.mvc.Request[play.api.mvc.AnyContent] => scala.concurrent.Future[play.api.mvc.Result])play.api.mvc.Action[play.api.mvc.AnyContent] <and>
[error] (block: => scala.concurrent.Future[play.api.mvc.Result])play.api.mvc.Action[play.api.mvc.AnyContent]
[error] cannot be applied to (play.api.mvc.Action[play.api.mvc.AnyContent])
[error] def create(name: String, age: Int) = Action.async {
这两个代码段不是在做同样的事情吗?如果没有,我如何提取方法的内容而不会出现错误?我想做的是从两个不同的地方调用提取的方法。
create
方法期望return类型是Future[Result]
,但是createPerson
return的调用是Action[AnyContent]
。您可以通过进行以下更改来更正它:
def create(name: String, age: Int) = Action.async {
createPerson(name,age).map(lastError =>
Ok("Mongo LastError: %s".format(lastError)))
}
def createPerson(name: String, age: Int):Future[String] = {
val json = Json.obj(
"name" -> name,
"age" -> age,
"created" -> new java.util.Date().getTime())
collection.insert(json)
}