ScalaJS 和 MongoDB 驱动程序:引用不存在的 class

ScalaJS and MongoDB driver: Referring to non-existent class

从昨天开始学习 Scala,我构建了一个与 MongoDB 索引交互的项目(工作正常)和另一个使用 ScalaJS 做同样事情的项目。

这是我的build.sbt:

enablePlugins(ScalaJSPlugin)

name := "demographics"

version := "1.0"

scalaVersion := "2.12.1"

libraryDependencies += "org.mongodb.scala" %% "mongo-scala-driver" % "1.2.1"

libraryDependencies += "be.doeraene" %%% "scalajs-jquery" % "0.9.1"
libraryDependencies += "org.scala-js" %%% "scalajs-dom" % "0.9.1"

skip in packageJSDependencies := false
jsDependencies +=
  "org.webjars" % "jquery" % "3.1.1" / "3.1.1/jquery.js"

和我的主要文件:

package demographics.webapp

import org.mongodb.scala._
import scala.scalajs.js.JSApp

object DemographicsApp extends JSApp {
  def main(): Unit = {
    // connect to the bdd
    val mongoClient: MongoClient = MongoClient()
    val database: MongoDatabase = mongoClient.getDatabase("demographics")
    val collection: MongoCollection[Document] = database.getCollection("demographics")
  }
}

非常基本:只需连接到集合。

遗憾的是,当我在 sbt shell 上启动 "fastOptJS" 时,出现以下错误:

[info] Compiling 1 Scala source to C:\...\target\scala-2.12\classes...
[info] Fast optimizing C:\...\target\scala-2.12\untitled1-fastopt.js
[error] Referring to non-existent class org.mongodb.scala.bson.DefaultHelper$DefaultsTo$
[error]   called from demographics.webapp.DemographicsApp$.main()scala.Unit
[error]   called from scala.scalajs.js.JSApp.$$js$exported$meth$main()java.lang.Object
[error]   called from demographics.webapp.PrenomApp$.$$js$exported$meth$main()java.lang.Object
[error]   called from demographics.webapp.DemographicsApp$.main
[error]   exported to JavaScript with @JSExport
[error] involving instantiated classes:
[error]   demographics.webapp.PrenomApp$
[error] Referring to non-existent class org.mongodb.scala.MongoClient$
[error]   called from demographics.webapp.DemographicsApp$.main()scala.Unit
[error]   called from scala.scalajs.js.JSApp.$$js$exported$meth$main()java.lang.Object
[error]   called from demographics.webapp.PrenomApp$.$$js$exported$meth$main()java.lang.Object
[error]   called from demographics.webapp.DemographicsApp$.main
[error]   exported to JavaScript with @JSExport
[error] involving instantiated classes:
[error]   demographics.webapp.DemographicsApp$
[error] Referring to non-existent class org.mongodb.scala.bson.collection.immutable.Document
[error]   called from demographics.webapp.DemographicsApp$.main()scala.Unit
[error]   called from scala.scalajs.js.JSApp.$$js$exported$meth$main()java.lang.Object
[error]   called from demographics.webapp.PrenomApp$.$$js$exported$meth$main()java.lang.Object
[error]   called from demographics.webapp.DemographicsApp$.main
[error]   exported to JavaScript with @JSExport
[error] involving instantiated classes:
[error]   demographics.webapp.DemographicsApp$
[error] Referring to non-existent method org.mongodb.scala.bson.DefaultHelper$DefaultsTo$.$default()org.mongodb.scala.bson.DefaultHelper$DefaultsTo
[error]   called from demographics.webapp.DemographicsApp$.main()scala.Unit
[error]   called from scala.scalajs.js.JSApp.$$js$exported$meth$main()java.lang.Object
[error]   called from demographics.webapp.PrenomApp$.$$js$exported$meth$main()java.lang.Object
[error]   called from demographics.webapp.DemographicsApp$.main
[error]   exported to JavaScript with @JSExport
[error] involving instantiated classes:
[error]   demographics.webapp.DemographicsApp$
[error] Referring to non-existent method org.mongodb.scala.MongoClient$.apply()org.mongodb.scala.MongoClient
[error]   called from demographics.webapp.DemographicsApp$.main()scala.Unit
[error]   called from scala.scalajs.js.JSApp.$$js$exported$meth$main()java.lang.Object
[error]   called from demographics.webapp.PrenomApp$.$$js$exported$meth$main()java.lang.Object
[error]   called from demographics.webapp.DemographicsApp$.main
[error]   exported to JavaScript with @JSExport
[error] involving instantiated classes:
[error]   demographics.webapp.DemographicsApp$
[trace] Stack trace suppressed: run last compile:fastOptJS for the full output.
[error] (compile:fastOptJS) There were linking errors

相同的代码(在主文件中进行了改编)正在 vanilla Scala 项目上运行,为什么会出现此错误?

它出现是因为您使用的是 JVM-only 库。 mongo-scala-driver 未针对 Scala.js 发布。确实如此,您可以使用 %%% 依赖项来依赖该库的 Scala.js 版本,它可以与 Scala.js 一起使用。但是您不能在 Scala.js 项目中使用 JVM-only 库。

(相反,您不能在 Scala/JVM 项目中使用仅 Scala.js 的库,例如 scalajs-dom。)