1

伙计们,我有一个小项目,我需要从会话中提取用户的 ID。

我不能把它放在 Text/Int 中,因为它说 Session 带有一个 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’
4

1 回答 1

2

用于keyToValues获取PersistValue值列表。

keyToValues :: Key record -> [PersistValue]

例如,如果您知道键是一个 Text 值,那么您的列表将包含一个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
   ...
于 2016-06-08T23:13:02.863 回答