簡體   English   中英

如何從(Yesod / Haskell項目中的會話中獲取用戶ID

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

伙計們,我有一個項目,我需要從Session中引出用戶的ID。

我不能將其放在Text / Int中,因為它說會話帶有密鑰(我認為是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’

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM