0

I am new to scala-play framework. I am using Play framework 2.5 and play-slick 1.1.1. I am confused in Future return type when using Action to handle request. I am getting this error:

found : scala.concurrent.Future[play.api.mvc.Result] required: play.api.mvc.Result

in my registerUser method.

This is my registerUser method

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)
        }**
      }
    }
  }
 )
}

Here is my table projection and method to insert data into user table. In table 'user', column 'email' is marked as a unique.

Table:

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)
}

method to insert in user table

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)
}

I also have method to check whether user with same email is exists or not. Here is the method

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

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

If I don't check for existing email through 'isExists' method then it throws SQLException for unique constraints.

Thank you very much in advance.

Govardhan
  • 35
  • 9

1 Answers1

0

The error is caused by the use of map on userDal.isExists(user.email). The if / else statement is generating a Future[Redirect], but the map call needs an instance of Redirect.

In this case, the easiest solution is to replace map with flatMap, which expects an instance of Future[Redirect].

jkinkead
  • 4,101
  • 21
  • 33