gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 09:12:29 25 4
gpt4 key购买 nike

我正在尝试使用以下代码在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com