当我用 ReactiveMongo 调用我的 mongo 实例时,我试图做一个理解。该方法应该检查是否返回任何结果,如果没有返回 Future(NotFound)。但是我遇到了一个我不明白的错误。
[info] Compiling 8 Scala sources and 1 Java source to /Users/Projects/reco_play/parser/target/scala-2.10/classes...
[error] /Users/Projects/reco_play/parser/app/controllers/Application.scala:94: value map is not a member of Object
[error] }.getOrElse(Future(NotFound))
[error] ^
[error] one error found
[error] (compile:compile) Compilation failed
进口:
import play.api.mvc._
import play.api.Play.current
import play.api.Logger
import play.modules.reactivemongo.{ReactiveMongoPlugin, MongoController}
import models.{Company}
import reactivemongo.api.collections.default.BSONCollection
import reactivemongo.bson.{BSONObjectID, BSONDocument}
import org.joda.time.DateTime
import scala.concurrent.{Future, ExecutionContext}
方法:
def showEditForm(id: String) = Action {
implicit request =>
implicit val reader = Company.CompanyReader
Async {
val objectId = new BSONObjectID(id)
val cursor = collection.find(BSONDocument("_id" -> objectId)).cursor[Company]
for {
maybeCompany <- cursor.headOption
result <- maybeCompany.map { company =>
Ok(views.html.editForm(Some(id), Company.form.fill(company)))
}.getOrElse(Future(NotFound))
} yield result
}
}