mongodb - 错误 : "could not find implicit value for parameter readFileReader" trying to save a file using GridFS with reactivemongo

标签 mongodb scala playframework-2.1 reactivemongo

我正在尝试使用以下代码在 Play 2.1 中使用 reactivemongo 保存附件:

def upload = Action(parse.multipartFormData) { request =>
    request.body.file("carPicture").map { picture =>
    val filename = picture.filename
    val contentType = picture.contentType

    val gridFS = new GridFS(db, "attachments")
    val fileToSave = DefaultFileToSave(filename, contentType)

    val futureResult: Future[ReadFile[BSONValue]] = gridFS.writeFromInputStream(fileToSave, new FileInputStream(new File(filename)))

    Ok(Json.obj("e" -> 0))
  }.getOrElse {
    Redirect(routes.Application.index).flashing(
      "error" -> "Missing file"
    )
  }
}

我收到以下错误:

找不到参数 readFileReader 的隐式值:reactivemongo.bson.BSONDocumentReader[reactivemongo.api.gridfs.ReadFile[reactivemongo.bson.BSONValue]] [错误] val futureResult: Future[ReadFile[BSONValue]] = gridFS.writeFromInputStream(fileToSave, new FileInputStream(new File(filename)))

我错过了什么?

谢谢,

遗传算法

最佳答案

很可能您在范围内没有 DefaultReadFileReader 隐式对象,您可以通过添加导入来修复:

import reactivemongo.api.gridfs.Implicits._

以下编译对我来说很好(使用 Play 2.1 reactivemongo 模块,版本 0.9):

package controllers

import java.io.{ File, FileInputStream }
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future

import play.api._
import play.api.mvc._
import play.api.libs.json._

import reactivemongo.api._
import reactivemongo.bson._
import reactivemongo.api.gridfs._
import reactivemongo.api.gridfs.Implicits._

import play.modules.reactivemongo.MongoController


object Application extends Controller with MongoController {

  def index = Action {
    Ok(views.html.index("Hello, world..."))
  }

  def upload = Action(parse.multipartFormData) { request =>
    request.body.file("carPicture").map { picture =>
      val filename = picture.filename
      val contentType = picture.contentType

      val gridFS = new GridFS(db, "attachments")
      val fileToSave = DefaultFileToSave(filename, contentType)

      val futureResult: Future[ReadFile[BSONValue]] = gridFS.writeFromInputStream(fileToSave, new FileInputStream(new File(filename)))

      Ok(Json.obj("e" -> 0))
    }.getOrElse {
      Redirect(routes.Application.index).flashing(
        "error" -> "Missing file"
      )
    }
  }
}

关于mongodb - 错误 : "could not find implicit value for parameter readFileReader" trying to save a file using GridFS with reactivemongo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18065172/

相关文章:

mongodb - 如何将 exec 函数的标准输出通过管道传递给另一个函数的读取器?

scala - 如何在 Scala 中创建非阻塞方法?

xml - Scala 2.9.x 中的 XML 支持状态

java - play2 在子模块/子项目中使用主项目 View / Controller /模型,反之亦然

unit-testing - Play 2.1 : unit testing EssentialActions

spring - 用 Spring 玩框架 2.1

linux - 脚本 linux mongodb 错误 SyntaxError : Unexpected [

MongoDB聚合框架: $project: use a value of a doc as the name of a key

mongodb - meteor - Mongo写入错误

scala - Scala Numeric[T] 的问题