scala - 无法找到参数写入错误的隐式值,但我使用宏定义了处理程序

标签 scala reactivemongo

我有以下内容:

帐户.scala

package modules.accounts

import java.time.Instant
import reactivemongo.api.bson._

case class Account(id: String, name: String)

object Account {
  type ID = String

  implicit val accountHandler: BSONDocumentHandler[Account] = Macros.handler[Account]

//   implicit def accountWriter: BSONDocumentWriter[Account] = Macros.writer[Account]
//   implicit def accountReader: BSONDocumentReader[Account] = Macros.reader[Account]
}

AccountRepo.scala

package modules.accounts

import java.time.Instant
import reactivemongo.api.collections.bson.BSONCollection

import scala.concurrent.ExecutionContext

final class AccountRepo(
    val coll: BSONCollection
)(implicit ec: ExecutionContext) {
  import Account.{ accountHandler, ID }

  def insertTest() = {
    val doc = Account(s"account123", "accountName") //, Instant.now)
    coll.insert.one(doc)
  }
}

我收到的错误是:

could not find implicit value for parameter writer: AccountRepo.this.coll.pack.Writer[modules.accounts.Account]
[error]     coll.insert.one(doc)

据我了解,由宏生成的隐式处理程序应该足够并创建 Writer。我做错了什么?

引用:http://reactivemongo.org/releases/1.0/documentation/bson/typeclasses.html

最佳答案

代码混合了不同的版本。

宏生成的处理程序使用新的 BSON API,可以通过 import reactivemongo.api.bson 看出,而集合使用旧的驱动程序,可以看出它使用 reactivemongo.api.collections.bson 而不是 reactivemongo.api.bson.collection

建议查看documentation ,并且不要混合相关库的不兼容版本。

关于scala - 无法找到参数写入错误的隐式值,但我使用宏定义了处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68238973/

相关文章:

scala - Akka actor 转发消息并继续

scala - 带有 Play Scala 的 ReactiveMongo

playframework - 创建一个 Play Enumeratee 将相似元素分组到具有最大大小的列表中

mongodb - Play 和 ReactiveMongo 嵌套 future 响应的编译器错误

scala - 如何正确打印rdd

scala:覆盖按名称调用代码块周围的隐式参数

java - 如何运行 Fast Scala Compiler 远程服务器?

scala - 如何在 Scala 中使用列表值转置 map

mongodb - 为什么reactivemongo的collection.save()会执行get()?