Search code examples
haskellyesod

Yesod - Extract session data (non-String), store it and use it


Hi there.

Here is the code I'm trying to make work :

getGameR :: Handler Html
getGameR = do
    sess <- getSession
    defaultLayout $ do
        setTitle "Game"
        $(widgetFile "hamletFile")
    where
        person = read $ fromJust $ Map.lookup "person" sess :: Person

data Person = Person
    {
        name :: Text
    }
    deriving (Show, Read)


The error is the following:

Handler/MyHandler.hs:87:56: Not in scope: `sess'


What I'm trying to do, is to extract data from Yesod Session (data of type Person) and store it inside 'person', to be able to use it inside the hamlet file.

Is there a way to get around that error?

If it's not possible, can you suggest another way around?

Thanks in advance.


Solution

  • sess is local to the do block, and thus it is not in scope in the person definition. As far as that error goes, using let inside the do block should be enough:

    getGameR :: Handler Html
    getGameR = do
        sess <- getSession
        let person = read $ fromJust $ Map.lookup "person" sess :: Person
        defaultLayout $ do
            setTitle "Game"
            $(widgetFile "hamletFile")