0

我是 scala-play 框架的新手。我正在使用 Play framework 2.5 和 play-slick 1.1.1。使用 Action 处理请求时,我对 Future 返回类型感到困惑。我收到此错误:

发现:scala.concurrent.Future[play.api.mvc.Result] 需要:play.api.mvc.Result

在我的registerUser方法中。

这是我的registerUser方法

def registerUser = Action.async { implicit request =>

registerForm.bindFromRequest.fold(

  errorForm => {
    Future.successful(Ok(views.html.registration(errorForm)))
  },

  user => {
    userDal.isExists(user.email).map { isExists =>

      if(isExists.booleanValue() == false){
        Redirect(routes.UserController.login()).flashing("message"->"Invalid Credential")
      }
      **else{
          userDal.registerUser(user.firstname, user.lastname, user.email, user.password, user.address).map { _ =>

          Redirect(routes.ProductController.index()).withSession("username" -> user.firstname)
        }**
      }
    }
  }
 )
}

这是我的表投影和将数据插入用户表的方法。在表 'user' 中,列 'email' 被标记为唯一的。

桌子:

private class UserDB(tag: Tag) extends Table[User](tag, "user") {

def id = column[Option[Int]]("id", O.PrimaryKey, O.AutoInc)

def firstname = column[String]("firstname")

def lastname = column[String]("lastname")

def email = column[String]("email")

def password = column[String]("password")

def addr = column[String]("address")

override def * = (id, firstname, lastname, email, password, addr) <> ((User.apply _).tupled, User.unapply)
}

插入用户表的方法

def registerUser(fname: String, lname: String, email: String, password: String, addr: String): Future[User] = db.run {

(userTable.map(u => (u.firstname, u.lastname, u.email, u.password, u.addr))

  returning userTable.map(_.id)
  // And we define a transformation for the returned value, which combines our original parameters with the

  into ((column, id) => User(id, column._1, column._2, column._3, column._4, column._5))) += (fname, lname, email, password, addr)
}

我也有方法检查是否存在具有相同电子邮件的用户。这是方法

def isExists(email: String): Future[Boolean] = db.run{

userTable.filter(_.email === email).exists.result
}

如果我不通过“isExists”方法检查现有电子邮件,那么它会针对唯一约束抛出 SQLException。

非常感谢您提前。

4

1 回答 1

0

该错误是由使用mapon引起的userDal.isExists(user.email)。该if / else语句正在生成Future[Redirect],但map调用需要 的实例Redirect

在这种情况下,最简单的解决方案是替换mapflatMap,它需要一个Future[Redirect].

于 2016-04-30T02:33:09.317 回答