Yesod persistent type error

99封情书 提交于 2019-12-10 16:45:48

问题


I am trying out persistent in a Yesod application. My model file contains

Job
 issuer MemberId
 addDate UTCTime
 lastDate UTCTime
 title Text
 description Text
 deriving Show Read

And my Handler:

getProfileR :: Handler RepHtml
getProfileR = do
 jobs <- runDB $ selectList [] [Desc JobAddDate]
 defaultLayout $ do
  setTitle "title"
  $(widgetFile "profile")

In profile.hamlet I loop trough the objects

$forall Job issuer addDate lastDate title description <- jobs
 <p>#{issuer}

However, I get the following error

Handler/Profile.hs:36:18:
    Couldn't match type `Entity' with `JobGeneric'
    In the return type of a call of `selectList'
    In the second argument of `($)', namely
      `selectList [] [Desc JobAddDate]'
    In a stmt of a 'do' block:
      jobs <- runDB $ selectList [] [Desc JobAddDate]

Handler/Profile.hs:36:18:
    Kind incompatibility when matching types:
      t0 :: (* -> *) -> * -> *
      JobGeneric Database.Persist.GenericSql.Raw.SqlPersist :: *
    In the return type of a call of `selectList'
    In the second argument of `($)', namely
      `selectList [] [Desc JobAddDate]'
    In a stmt of a 'do' block:
      jobs <- runDB $ selectList [] [Desc JobAddDate]
Build failure, pausing...

Where line 36 is the runDB line.

Being new to Haskell, I can't figure out whats wrong. I am following the Yesod Book. They are unfortunately avoiding the Scaffolded Site so I can't completely mimic their code.


回答1:


selectList does not return [Job], it's actually [Entity Job] which contains both the Job and its Key*

There are a number of ways to refactor this to handle it, one would be:

$forall Entity jobId job <- jobs
    <p>#{jobIssuer job}

In your template.

Alternatively, you can use map entityVal at any point to turn [Entity Job] -> [Job] if you'd prefer to work with that.

*The Entity and Key types are actually a bit more complex, but I find it's easier to think about them this way. Please read the docs if you're interested.



来源:https://stackoverflow.com/questions/11285419/yesod-persistent-type-error

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