gpt4 book ai didi

postgresql - Scala + Play -> 类型不匹配;找到 : anorm. RowParser Required : anorm. ResultSetParser[Option[models.User]]

转载 作者:行者123 更新时间:2023-11-29 12:47:30 27 4
gpt4 key购买 nike

解决方案:我不知道如何返回 Option[User] 的不存在,所以在没有找到用户的情况下,我创建了一个虚拟用户对象并从 Controller 对其进行推理(感觉很糟糕但有效。 ..):来自 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
})

)

++++++++++++++++++ 原版 2++++++++++++++++++感谢您的建议!我做了一些更改并且似乎越来越接近但是我无法弄清楚如何返回未定义的选项[用户]。我也尝试过 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并搜索了几个小时......任何帮助将不胜感激!

最佳答案

您没有指定要映射的行。在您的行映射器之后,放置一个 * 来表示要映射的行。当您使用它时,我发现在单独的 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”,以及一个“*”,表示您要映射所有行。

关于postgresql - Scala + Play -> 类型不匹配;找到 : anorm. RowParser Required : anorm. ResultSetParser[Option[models.User]],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11724368/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com