2016-07-28 70 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]