我正在尝试使用一些附加功能扩展Happstack 速成课程博客:在主页上显示所有标签的列表。
我的博客记录是这样的:
data Blog = Blog
{ nextPostId :: PostId
, posts :: IxSet Post
, allTags :: [Text]
}
deriving (Data, Typeable)
我通过以下方式通过 id 获取博客文章(从速成课程中复制粘贴):
-- Models.hs
postById :: PostId -> Query Blog (Maybe Post)
postById pid =
do Blog{..} <- ask
return $ getOne $ posts @= pid
-- Controller.hs
viewPage :: AcidState Blog -> ServerPart Response
viewPage acid =
do pid <- PostId <$> lookRead "id"
mPost <- query' acid (PostById pid)
...
-- mPost has type Maybe Post here
...
它工作正常。
当我尝试以类似方式查询所有标签时:
-- Models.hs
getTags :: Query Blog [Text]
getTags =
do Blog{..} <- ask
return allTags
-- Controller.hs
serveTags :: AcidState Blog -> [Text]
serveTags acid = query' acid GetTags
它行不通。错误堆栈跟踪是:
Blog/Controllers.hs:154:18:
Couldn't match type `[Text]' with `Text'
Expected type: [Text]
Actual type: [acid-state-0.8.1:Data.Acid.Common.EventResult
GetTags]
In the return type of a call of query'
In the expression: query' acid GetTags
我不知道为什么返回类型query'
是[EventResult GetTags]
,而应该是[Text]
。
这个错误的原因是什么?有没有办法解决它?