尝试将数据插入MongoDB时出错

尝试将数据插入MongoDB时出错,mongodb,scala,reactivemongo,Mongodb,Scala,Reactivemongo,我正在尝试使用Play scala和ReactiveMongo将数据插入MongoDB 这是我的DbimpService.scala: package services import models.Post import reactivemongo.bson.BSONDocument import reactivemongo.api.MongoDriver import reactivemongo.api.collections.bson.BSONCollection import scala.

我正在尝试使用Play scala和ReactiveMongo将数据插入MongoDB

这是我的
DbimpService.scala

package services
import models.Post
import reactivemongo.bson.BSONDocument
import reactivemongo.api.MongoDriver
import reactivemongo.api.collections.bson.BSONCollection
import scala.concurrent.ExecutionContext
import javax.inject.Inject
import play.api.libs.json.Json
import reactivemongo.play.json.collection.JSONCollection
import reactivemongo.api.commands.WriteResult
import scala.concurrent.Future
import org.apache.xerces.util.DatatypeMessageFormatter

class Dbimpservice @Inject() (implicit ec:ExecutionContext) extends Dbservice {
    def create(p:Post):String={
            var status = "Not Saved"
                    val driver = new MongoDriver
                    val connection = driver.connection(List("localhost"))
                    val db = connection("application")
                    val collection = db[BSONCollection]("post")
                    val futureList = collection.insert[Post](p)
                    futureList.onComplete { case sucess => println(sucess) }
            return status
    }
}
package controllers

import javax.inject._
import play.api._
import play.api.mvc._
import models._
import scala.util.{ Failure, Success }
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
import reactivemongo.api.{ MongoDriver, MongoConnection }
import reactivemongo.play.json.collection.JSONCollection
import reactivemongo.bson.BSONDocument
import reactivemongo.api.commands.WriteResult
import reactivemongo.api.collections.bson.BSONCollection
import play.api.libs.json.Json
import services.Dbservice
import services.Dbimpservice
import services.Dbservice
import scala.concurrent.ExecutionContext
import scala.concurrent.Await
import scala.concurrent.duration.Duration

/**
 * This controller creates an `Action` to handle HTTP requests to the
 * application's home page.
 */
@Singleton
class HomeController @Inject() (implicit ec:ExecutionContext,val Dbservice : Dbimpservice)extends Controller {

    /**
     * Create an Action to render an HTML page with a welcome message.
     * The configuration in the `routes` file means that this method
     * will be called when the application receives a `GET` request with
     * a path of `/`.
     */
  def index  = Action{
    Ok("Hai")
  }

    def read = Action.async {
      val query = BSONDocument()
                    val driver = new MongoDriver
                    val connection = driver.connection(List("localhost:27017"))
                    val db = connection("application")
                    val collection = db[BSONCollection]("post")
                    val futureList = collection.find(query).cursor[Post]().collect[List]()
                    futureList.map { list =>
              Ok(list.toString())
          }
    }

    def create = Action(BodyParsers.parse.json) { request => 
     val personResult = request.body.validate[Post]
    personResult.fold(
      errors => {
        BadRequest(Json.obj("status " ->"ERROR"))
      },
      valid = fun
    )
   }

    def fun:Post => Result= { post =>
      var ans = Dbservice.create(post)
      Ok(ans)
  }

}
这是我的
家庭控制器。scala

package services
import models.Post
import reactivemongo.bson.BSONDocument
import reactivemongo.api.MongoDriver
import reactivemongo.api.collections.bson.BSONCollection
import scala.concurrent.ExecutionContext
import javax.inject.Inject
import play.api.libs.json.Json
import reactivemongo.play.json.collection.JSONCollection
import reactivemongo.api.commands.WriteResult
import scala.concurrent.Future
import org.apache.xerces.util.DatatypeMessageFormatter

class Dbimpservice @Inject() (implicit ec:ExecutionContext) extends Dbservice {
    def create(p:Post):String={
            var status = "Not Saved"
                    val driver = new MongoDriver
                    val connection = driver.connection(List("localhost"))
                    val db = connection("application")
                    val collection = db[BSONCollection]("post")
                    val futureList = collection.insert[Post](p)
                    futureList.onComplete { case sucess => println(sucess) }
            return status
    }
}
package controllers

import javax.inject._
import play.api._
import play.api.mvc._
import models._
import scala.util.{ Failure, Success }
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
import reactivemongo.api.{ MongoDriver, MongoConnection }
import reactivemongo.play.json.collection.JSONCollection
import reactivemongo.bson.BSONDocument
import reactivemongo.api.commands.WriteResult
import reactivemongo.api.collections.bson.BSONCollection
import play.api.libs.json.Json
import services.Dbservice
import services.Dbimpservice
import services.Dbservice
import scala.concurrent.ExecutionContext
import scala.concurrent.Await
import scala.concurrent.duration.Duration

/**
 * This controller creates an `Action` to handle HTTP requests to the
 * application's home page.
 */
@Singleton
class HomeController @Inject() (implicit ec:ExecutionContext,val Dbservice : Dbimpservice)extends Controller {

    /**
     * Create an Action to render an HTML page with a welcome message.
     * The configuration in the `routes` file means that this method
     * will be called when the application receives a `GET` request with
     * a path of `/`.
     */
  def index  = Action{
    Ok("Hai")
  }

    def read = Action.async {
      val query = BSONDocument()
                    val driver = new MongoDriver
                    val connection = driver.connection(List("localhost:27017"))
                    val db = connection("application")
                    val collection = db[BSONCollection]("post")
                    val futureList = collection.find(query).cursor[Post]().collect[List]()
                    futureList.map { list =>
              Ok(list.toString())
          }
    }

    def create = Action(BodyParsers.parse.json) { request => 
     val personResult = request.body.validate[Post]
    personResult.fold(
      errors => {
        BadRequest(Json.obj("status " ->"ERROR"))
      },
      valid = fun
    )
   }

    def fun:Post => Result= { post =>
      var ans = Dbservice.create(post)
      Ok(ans)
  }

}
我正在尝试插入数据,但没有被插入,我得到的错误是

Failure(reactivemongo.core.errors.ConnectionNotInitialized: MongoError['Connection is missing metadata (like protocol version, etc.) The connection pool is probably being initialized.'])
有人请帮助我,我甚至提到了链接

http://stackoverflow.com/questions/31456517/embedmongo-with-reactivemongo-process-does-not-exit

但是没有猜到您正在使用最新版本的ReactiveMongo(0.11.7+),您正在使用一个代码(
connection(dbName)
aka
connection.apply(dbName)

您需要使用异步解析,这将从故障转移中获益(以处理可能的网络延迟/事件)

val db = connection("application")
val collection = db[BSONCollection]("post")
val futureList = collection.insert[Post](p)
使用新的:

用于{

db猜测您正在使用最新版本的ReactiveMongo(0.11.7+),您正在使用一个代码(
connection(dbName)
aka
connection.apply(dbName)

您需要使用异步解析,这将从故障转移中获益(以处理可能的网络延迟/事件)

val db = connection("application")
val collection = db[BSONCollection]("post")
val futureList = collection.insert[Post](p)
使用新的:

用于{

db您需要指示db和lib的版本您需要指示db和lib的版本