Error for first request

24 views
Skip to first unread message

Zoro

unread,
Jun 1, 2016, 4:06:18 PM6/1/16
to ReactiveMongo - http://reactivemongo.org
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 

Cédric Chantepie

unread,
Jun 2, 2016, 3:17:47 AM6/2/16
to ReactiveMongo - http://reactivemongo.org
Use reactiveMongoApi.database (which returns a `Future[DefaultDB]` to be composed with your ops) rather than `reactiveMongoApi.db` (which will be deprecated).

Хандрамай Павел

unread,
Jun 2, 2016, 5:11:35 AM6/2/16
to reacti...@googlegroups.com
Thanks! It works.

On Thu, Jun 2, 2016 at 10:17 AM, Cédric Chantepie <chantep...@gmail.com> wrote:
Use reactiveMongoApi.database (which returns a `Future[DefaultDB]` to be composed with your ops) rather than `reactiveMongoApi.db` (which will be deprecated).

--
You received this message because you are subscribed to a topic in the Google Groups "ReactiveMongo - http://reactivemongo.org" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/reactivemongo/n47c6gFRr8Y/unsubscribe.
To unsubscribe from this group and all its topics, send an email to reactivemong...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
С уважением,
Хандрамай Павел Александрович
Reply all
Reply to author
Forward
0 new messages