Hello
I using Play 2.5.3, play2-reactivemongo 0.11.11 and mongod 3.2.6 with SCRAM-SHA-1
Application can record new person and show the list of all persons.
package controllers
import java.util.Date
import javax.inject.Inject
import play.api.libs.json.{JsArray, JsObject, Json}
import play.api.mvc._
import play.modules.reactivemongo.{MongoController, ReactiveMongoApi, ReactiveMongoComponents}
import reactivemongo.api.Cursor
import reactivemongo.play.json._
import reactivemongo.play.json.collection.JSONCollection
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
class Application @Inject()(
val reactiveMongoApi: ReactiveMongoApi
) extends Controller with MongoController with ReactiveMongoComponents {
def collection: JSONCollection = reactiveMongoApi.db.collection[JSONCollection]("persons")
def index = Action {
Ok("works")
}
def create(name: String, age: Int) = Action.async {
val json = Json.obj(
"name" -> name,
"age" -> age,
"created" -> new Date().getTime()
)
collection.insert(json).map(lastError =>
Ok("Mongo LastError: %s".format(lastError)))
}
def findByName(name: String) = Action.async {
val cursor: Cursor[JsObject] = collection.
find(Json.obj("name" -> name)).
sort(Json.obj("created" -> -1)).
cursor[JsObject]
val futurePersonsList: Future[List[JsObject]] = cursor.collect[List]()
val futurePersonsJsonArray: Future[JsArray] =
futurePersonsList.map { persons => Json.arr(persons) }
futurePersonsJsonArray.map { persons =>
Ok(persons)
}
}
}
routes file
# Routes
# This file defines all application routes (Higher priority routes first)
# ~~~~
# Home page
GET / controllers.Application.index
GET /create controllers.Application.create(name ?= "noname", age:Int ?= 0)
GET /list controllers.Application.findByName(name ?= "noname")
# Map static resources from the /public folder to the /assets URL path
GET /assets/*file controllers.Assets.at(path="/public", file)
After start home page (/) loading without a problem. When i try load /create (first write to DB) first time, i get
[ConnectionNotInitialized: MongoError['Connection is missing metadata (like protocol version, etc.) The connection pool is probably being initialized.']]
After refresh the page, all works as expected. What could be a problem?
I tested it a lot. If i load /list first, and then /create, i also don't have error.
If i change application.conf from
mongodb.uri = "mongodb://test:gfhjkm@localhost:27017/test?authMode=scram-sha1"
to
mongodb.uri = "mongodb://test:gfhjkm@localhost:27017/test"
i get error every time i try load /create, and never when load /list.
Full code here
https://github.com/n0tel/reactivemongo-test. Just create test database with test user and "gfhjkm" password for reproduce.
I appreciate any help