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

时间:2017-01-07 15:00:15

标签: mongodb scala sbt scala.js

从昨天开始学习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")
  }
}

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

可悲的是,当我发布一个" fastOptJS"在sbt shell上,出现以下错误:

[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项目,为什么会出现此错误?

1 个答案:

答案 0 :(得分:1)

看起来是因为您使用的是仅限JVM的库。不为Scala.js发布mongo-scala-driver。它是,您可以使用%%%依赖项来依赖该库的Scala.js版本,该版本适用于Scala.js。但是您不能在Scala.js项目中使用仅JVM库。

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