如何从Session获取用户ID(Yesod / Haskell Project

时间:2016-06-08 22:59:51

标签: haskell yesod yesod-forms

伙计们,我得到了一个小程序,我需要从会话中提取用户ID。

我不能把它放在Text / Int中因为它说会话带有一个Key(我认为是Sql Key)如何将它转换为Int以用于我项目中的其他方法

我试图从会话中恢复ID

getInicioR :: Handler Html
getInicioR = do
        uid <- lookupSession "_ID"
        user <- runDB $ get404 uid 

显示以下错误消息:

Couldn't match expected type ‘Key t0’ with actual type ‘Maybe Text’
In the first argument of ‘get404’, namely ‘uid’
In the second argument of ‘($)’, namely ‘get404 uid’

1 个答案:

答案 0 :(得分:2)

使用keyToValues获取PersistValue值列表。

keyToValues :: Key record -> [PersistValue]

例如,如果您知道密钥是文本值,那么您的列表将包含一个PersistText值,您可以这样继续:

do uid <- lookupSession "_ID"
   let pvals = keyToValues uid
       [ PersistText txt ] = pvals
   liftIO $ print pvals            -- to see what pvals is
   -- now txt is a Text value
   ...
相关问题