如何从(Yesod/Haskell Project [英] How get the user ID from the Session at (Yesod / Haskell Project
本文介绍了如何从(Yesod/Haskell Project的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
伙计,我有一个项目,我需要从会话中引出该用户的ID.
guys i got a little projet and i need to extrat de ID of the user from the Session.
我不能将其放在Text/Int中,因为它说会话带有密钥(我认为是Sql Key),我如何将其转换为Int以在项目中的其他方法中使用
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
我试图这样做是为了从会话中恢复ID
I Tried to do it to recover the ID from session
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’
推荐答案
Use keyToValues
to get a list of PersistValue
values.
keyToValues :: Key record -> [PersistValue]
例如,如果您知道键是一个Text值,那么您的列表将包含一个 PersistText
值,您可以这样进行:
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
...
这篇关于如何从(Yesod/Haskell Project的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文