我正在 Yesod 应用程序中尝试持久化。我的模型文件包含
Job
issuer MemberId
addDate UTCTime
lastDate UTCTime
title Text
description Text
deriving Show Read
还有我的处理程序:
getProfileR :: Handler RepHtml
getProfileR = do
jobs <- runDB $ selectList [] [Desc JobAddDate]
defaultLayout $ do
setTitle "title"
$(widgetFile "profile")
在 profile.hamlet 中,我循环遍历对象
$forall Job issuer addDate lastDate title description <- jobs
<p>#{issuer}
但是,我收到以下错误
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...
其中第 36 行是 runDB 行。
作为 Haskell 的新手,我不知道出了什么问题。我正在关注 Yesod Book。不幸的是,他们避开了脚手架网站,所以我无法完全模仿他们的代码。
selectList
不返回[Job]
,实际上是[Entity Job]
其中包含Job
和它的Key
*
有多种方法可以重构此问题来处理它,其中一种是:
$forall Entity jobId job <- jobs
<p>#{jobIssuer job}
在你的模板中。
或者,您可以使用map entityVal
在任何一点转弯[Entity Job] -> [Job]
如果您愿意使用它。
*The Entity
and Key
类型实际上有点复杂,但我发现这样思考它们更容易。如果您有兴趣,请阅读文档。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)