2016-07-28 66 views
0

我有以下功能:功能与未来的返回类型总是返回无

//retrieves record from database 
def getAll: Future[List[User]] = { 
    try { 
    Logger.info("Getting all user record.") 
    db.run(userTableQuery.to[List].result) 
    } 
    catch { 
    case ex: Exception => Logger.error("Exception in getting all user record. " + ex) 
     Future { 
     List[User]() 
     } 
    } 
} 


//checks whethe the email exist in list or not 
def checkEmail(email : String): Future[Option[User]] ={ 
    /* userRepo.getAll.map(userList => userList.filter(user => user.email == email).map { value => println(value) 
    userList 
    })*/ 
    userRepo.getAll.map(userList => userList.filter(user => user.email == email).headOption) 

} 


//allows to sign in and redirects to dashboard 
def signIn() = { 
    Logger.debug("signingIn in progress. ") 
    loginForm.bindFromRequest.fold(
    formWithErrors => { 
     Logger.error("Sign-In badRequest.") 
     Future(BadRequest(views.html.home(webJarAssets, formWithErrors, signUpForm))) 
    }, 
    validData => { 
     userService.checkEmail(validData.email).map(value => { 
     value match { 
      case Some(us) =>Redirect(routes.HomeController.homePage).flashing("ERROR" -> "User exist") 
      case None => Redirect(routes.HomeController.homePage).flashing("ERROR" -> "User doesn't exist") 
     } 
     } 
    ) 
    } 
) 
} 

但是当我打电话signin()它总是返回None

我使用了一些调试器代码,我猜0123¾里面的checkMail()工作不正常。 但getall()正常工作并给出数据库中的所有记录。

+1

那么问题是什么? –

+0

userRepo.getAll返回什么? userRepo是您正在显示其功能的类的实例吗? – sascha10000

回答

0

我认为问题在于如何将用户电子邮件与checkMail()函数中的过滤器内提供的用户电子邮件进行比较。 字符串相等性有点棘手,如果您使用==比较它们,那么您将比较对象而不是值,因此您应该使用.equals()来比较值。 你可以阅读更多关于这个blog post

尝试重写checkMail()这样的:

def checkEmail(email : String): Future[Option[User]] ={ 
     userRepo.getAll.map(userList => userList.filter(user => user.email.equals(email)).headOption) 
    } 

您还可以简化.filter()电子.headOption使用find(),它只有同样的事情在一个命令。你可以像这样改写它:

def checkEmail(email : String): Future[Option[User]] ={ 
     userRepo.getAll.map(userList => userList.find(user => user.email.equals(email))) 
    } 
0

代替使用过滤器,你可以在checkmail下使用find方法。而且,由于这是斯卡拉您正在使用“==”正确,请参阅博客here

我希望这个代码将解决:

//checks whethe the email exist in list or not 
def checkEmail(email : String): Future[Option[User]] ={ 
    /* userRepo.getAll.map(userList => userList.filter(user => user.email == email).map { value => println(value) 
    userList 
    })*/ 
    userRepo.getAll.map(userList => userList.find(user => user.email == email)) 

} 

我试图模拟关于直接的方式您的实现/实验使用终端:

scala> case class User(email: String) 
defined class User 

scala> import scala.concurrent.ExecutionContext.Implicits.global 
import scala.concurrent.ExecutionContext.Implicits.global 

scala> val allUser = scala.concurrent.Future {List(User("[email protected]"), User("[email protected]"), User("[email protected]"))} 
allUser: scala.concurrent.Future[List[User]] = [email protected] 

scala> val checkmail = allUser.map(userlist=>userlist.find(user=>user.email == "[email protected]")) 
checkmail: scala.concurrent.Future[Option[User]] = [email protected] 

scala> val rslt = checkmail.map(value => value match {case Some(x) =>println(x); x.email case None => println("None"); "nothing" }) 
rslt: scala.concurrent.Future[Unit] = [email protected] 
User([email protected]) 

scala> import scala.concurrent.duration._ 
import scala.concurrent.duration._ 


scala> import scala.concurrent._ 
import scala.concurrent._ 

scala> Await.result(rslt, 3 seconds) 
warning: there was one feature warning; re-run with -feature for details 
res8: String = [email protected]