How get the user ID from the Session at (Yesod / Haskell Project

二次信任 提交于 2019-12-11 08:17:27

问题


guys i got a little projet and i need to extrat de ID of the user from the Session.

I can't put it in a Text/Int because it says that the Session carry an Key (Sql Key i think) how can i converte it to Int to use in other methods from my project

I Tried to do it to recover the ID from session

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

Shows the follow error message:

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:


Use keyToValues to get a list of PersistValue values.

keyToValues :: Key record -> [PersistValue]

If you know, for instance, that the key is a Text value, then your list will consist of a single PersistText value and you could proceed like this:

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
   ...


来源:https://stackoverflow.com/questions/37714297/how-get-the-user-id-from-the-session-at-yesod-haskell-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!