mongodbscalacasbahsalat

Problems with Salat methods in MongoDB: implicit view & not enough arguments


I'm new with Salat,Casbah and MongoDB. When I've been trying to make a simple method to get all users from db,

 import DAL.Instances.User.{UserDAO, User}
    import com.novus.salat._
    import com.novus.salat.global._
    import com.novus.salat.annotations._
    import com.novus.salat.dao._
    import com.mongodb.casbah.Imports._
    import com.mongodb.casbah.MongoConnection



    object UserRepository {

        def getAllUsers() = {

        val userList= UserDAO.find()

        userList.isEmpty match {
          case true => throw new Exception("None users in your db")
          case false => userList
        }
}

I faced with two errors:

Error:(29, 31) No implicit view available from Unit => com

.mongodb.DBObject.
        val userList= UserDAO.find()
                                  ^
    Error:(29, 31) not enough arguments for method find: (implicit evidence$2: Unit => com.mongodb.DBObject)com.novus.salat.dao.SalatMongoCursor[DAL.Instances.User.User].
Unspecified value parameter evidence$2.
    val userList= UserDAO.find()
                              ^

Here is my User code:

object User {
  case class User( _id: ObjectId = new ObjectId, name:String, age:Int)
  object UserDAO extends SalatDAO[User, ObjectId](collection = MongoConnection()("fulltestdb")("user"))

}

Solution

  • I'm not sure what version of Salat you are using but if you look at the signature for find it'll give you a clue as to what the issue is:

    def find[A <% DBObject](ref: A): SalatMongoCursor[ObjectType]
    

    You need to call find with a parameter that has a view bound so that this parameter may be viewed as a DBObject. This means that an implicit conversion from A => DBObject is expected to be in scope.

    In your case you aren't passing any parameter. This is being treated as Unit and so the compiler tries to find an implicit conversion from Unit => DBObject. This can't be found so compilation fails.

    To fix this you're best bet is to pass in an empty DBObject, you can achieve this with MongoDBObject.empty from casbah. You could add an implicit conversion from Unit => MongoDBObject but I'd probably lean towards making it explicit where possible.