使用Scala Play 2.4 ReactiveMongo的DatabaseException

时间:2016-01-03 16:03:12

标签: mongodb scala playframework-2.0 reactivemongo

我创建了一个简单的Play-Scala应用程序来测试ReactiveMongo并遇到一个奇怪的异常。这些是步骤:

  1. 制作新的Play-scala应用

    activator new test-mongo

  2. 根据此链接配置application.conf和build.sbt( http://reactivemongo.org/releases/0.11/documentation/tutorial/play2.html

  3. 修改控制器Application.scala

  4. 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. 运行应用程序并浏览应用程序页面。首次运行时,将出现以下异常。刷新页面,将显示数据。
    2. [DetailedDatabaseException: DatabaseException['not authorized for query on posts.posts' (code = 13)]]
      
      1. 以下显示所用软件的版本:
      2. Scala: 2.11.6 
        Play: 2.4 
        Mongo: 3.0.7
        ReactiveMongo: 0.11.9
        

1 个答案:

答案 0 :(得分:0)

使用play2-reactivemongo 0.12.0-SNAPSHOT时问题解决了。但是,每次运行之前都必须“清理”代码。