Scala + Play - >类型不匹配;发现:anorm.RowParser必需:anorm.ResultSetParser [Option [models.User]]

时间:2012-07-30 15:16:51

标签: postgresql scala playframework

解决方案:我无法弄清楚如何返回Option [User]的非存在,所以在未找到用户的情况下,我从控制器创建一个虚拟用户对象及其原因(感觉很糟糕但工作正常)。 ..): 来自Application.scala

val loginForm = Form(
tuple(
  "email" -> text,
  "password" -> text
) verifying ("Invalid email or password", result => result match {
  case (email, password)  => (User.authenticate(email, password).map{_.id}.getOrElse(0) != 0)
})

反对:

val loginForm = Form(
  tuple(
  "email" -> text,
  "password" -> text
) verifying ("Invalid email or password", result => result match {
  case (email, password) => User.authenticate(email, password).isDefined
})

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 感谢您的建议!我做了一些改变,似乎越来越近,但我无法弄清楚如何返回一个未完成的Option [user]。我也试过case _ => null,见下文:

来自User.scala

case class User(id: Int, email: String, name: String, password: String)

object User {

  // -- Parsers

  /**
   * Parse a User from a ResultSet
   */
  val userParser = {
            get[Option[Int]]("uid")~        
            get[Option[String]]("email")~
            get[Option[String]]("fname")~
            get[Option[String]]("pbkval") map {
            case (uid~email~name~pbkval) => validate(uid,email, name, pbkval)
            }
  }

  /**
   * Retrieve a User from email.
   */
  def findByEmail(email: String): Option[User] = {
    DB.withConnection { implicit connection =>
      SQL("select * from get_pbkval({email})").on(
                'email -> email         
            ).as(userParser.singleOpt)
    }
  }


  /**
   * Authenticated user session start.
   */
  def authenticate(email: String, password: String): Option[User] = {
    DB.withConnection { implicit connection =>
      SQL(
            """
                        select * from get_pbkval({email})
            """
            ).on(
                'email -> email
            ).as(userParser.singleOpt)

        }
    }

  /**
   * Validate entry and create user object.
   */
    def validate(uid: Option[Int], email: Option[String], fname: Option[String], pbkval: Option[String]): User = {
                val uidInt : Int = uid.getOrElse(0)
                val emailString: String = email.getOrElse(null)
                val fnameString: String = fname.getOrElse(null)
                val pbkvalString: String = pbkval.getOrElse(null)
                User(uidInt, emailString, fnameString, pbkvalString)
    }

我想很明显,我并没有真正得到一些基本的东西......我已经阅读了http://www.playframework.org/modules/scala-0.9.1/anorm并搜索了几个小时......任何帮助都将非常感谢!

1 个答案:

答案 0 :(得分:3)

您没有指定要映射的行。在行映射器之后,添加*以表示要映射的行。当你在它的时候,我发现在一个单独的val中定义我的行映射器更容易。这样的事情。

 val user = { 
   get[Option[Int]]("uid")~        
   get[Option[String]]("email")~
   get[Option[String]]("fname")~
   get[Option[String]]("pbkval") map {
    case uid~email~name~password => validate(uid,email, name, password)
  }
}

def authenticate(email: String, password: String): Option[User] = {
DB.withConnection { implicit connection =>
  SQL(
    """
        select * from get_pbkval({email})
    """
    ).on(
        'email -> email
    ).as(user *)
  }
 } 

    def validate(uid: Option[Int], email: Option[String], fname: Option[String], pbkval: Option[String]): Option[User] = {
    if (uid != None) {
            val uidInt : Int = uid.getOrElse(0)
            val emailString: String = email.getOrElse(null)
            val fnameString: String = fname.getOrElse(null)
            val pbkvalString: String = pbkval.getOrElse(null)
            User(uidInt, emailString, fnameString, pbkvalString)
    } else { return null}
}

请注意,“as”方法现在有两个参数,即行映射器(现在定义为val“user”)和“*”表示您要映射所有行。