简体   繁体   中英

DatabaseException with Scala Play 2.4 ReactiveMongo

I created a simple Play-Scala application to test ReactiveMongo and encountered a strange exception. These are the steps:

  1. Create a new play-scala app

    activator new test-mongo

  2. Configure application.conf and build.sbt according to this link ( http://reactivemongo.org/releases/0.11/documentation/tutorial/play2.html )

  3. Modify the controller Application.scala

 package controllers import play.api._ import play.api.mvc._ import javax.inject.Inject import play.api.mvc.Controller import scala.concurrent.{ ExecutionContext, Future } import scala.concurrent.ExecutionContext.Implicits.global import play.api.Play.current import play.api.libs.json._ import reactivemongo.bson.BSONDocument import reactivemongo.api.commands.WriteResult import reactivemongo.api.Cursor import play.modules.reactivemongo._ import play.modules.reactivemongo.json._ import play.modules.reactivemongo.json.collection.JSONCollection import play.modules.reactivemongo.ReactiveMongoApi class Application @Inject() (val reactiveMongoApi: ReactiveMongoApi) extends Controller with MongoController with ReactiveMongoComponents { def collection: JSONCollection = db.collection[JSONCollection]("posts") def index = Action.async { val cursor: Cursor[JsObject] = collection. // find all people with name `name` find(Json.obj("username" -> "Rob")). // sort them by creation date sort(Json.obj("created" -> -1)). // perform the query and get a cursor of JsObject cursor[JsObject] // gather all the JsObjects in a list val futurePersonsList: Future[List[JsObject]] = cursor.collect[List]() // transform the list into a JsArray val futurePersonsJsonArray: Future[JsArray] = futurePersonsList.map { persons => Json.arr(persons) } // everything's ok! Let's reply with the array futurePersonsJsonArray.map { persons => // Ok(views.html.index("Your new application is ready." + persons)) Ok(persons) } } } 
  1. Run the app and browse the application page. On first run, the following exception will appear. Refresh the page and the data will be displayed.
 [DetailedDatabaseException: DatabaseException['not authorized for query on posts.posts' (code = 13)]] 
  1. The following shows the versions of the software used:
 Scala: 2.11.6 Play: 2.4 Mongo: 3.0.7 ReactiveMongo: 0.11.9 

Problem solved when using play2-reactivemongo 0.12.0-SNAPSHOT. However, the code has to be "cleaned" every time before run.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM