eje211
eje211

Reputation: 2429

Dependency injection issue for ReactiveMongo in Play 2

I'm building a web app using Play 2.6 and ReactiveMongo 0.13. But when I try to run the app, I get this output:

ProvisionException: Unable to provision, see the following errors:

1) No implementation for play.modules.reactivemongo.ReactiveMongoApi was bound.
  while locating play.modules.reactivemongo.ReactiveMongoApi
    for the 2nd parameter of controllers.Employees.<init>(Employees.scala:17)
...

1 error

The class this error refers to is declared with this constructor:

class Employees @Inject() (cc: ControllerComponents,
                           val reactiveMongoApi: ReactiveMongoApi,
                           implicit val materializer: akka.stream.Materializer
                          )

ReactiveMongo is specified and is supposed to be injected.

I read on the web that the solution is to add the following line to application.conf:

play.modules.enabled += "play.modules.reactivemongo.ReactiveMongoModule"

The thing is that I did that, but I still get the dependency injection error. I'm not sure where to proceed from here.

Upvotes: 0

Views: 279

Answers (1)

eje211
eje211

Reputation: 2429

I was given help on another forum as to how to solve this. I was missing a configuration option to select the MongoDB database. The database selection option in Play is db but ReactiveMongo uses its own syntax. I had to add this to application.conf:

mongodb.uri="mongodb://localhost/collectionname"

Adding that line caused the dependency injection to activate.

I also had to leave:

play.modules.enabled += "play.modules.reactivemongo.ReactiveMongoModule"

Both are needed, even though the second one is not in the documentation.

Upvotes: 1

Related Questions