我有一个 Yesod 处理程序,它返回 [(Category, [Product])] 类型的列表,我试图在我的 hamlet 模板中循环遍历它。
$if null rows
<p>No products
$else
<div class="list-group menu">
$forall (category, products) <- rows
<h4>#{categoryName category}
$forall product <- products
<p>#{productName product} - #{productPrice product}</p>
当我编译时,虽然我收到错误消息:
Handler/Menu.hs:11:7:
Couldn't match type ‘[(Category, [Product])]’
with ‘(Category, t0 Product)’
Expected type: ReaderT SqlBackend m1 (Category, t0 Product)
Actual type: ReaderT SqlBackend m1 [(Category, [Product])]
In the second argument of ‘Data.Foldable.mapM_’, namely ‘rows’
In a stmt of a 'do' block:
Data.Foldable.mapM_
(\ (category_aiv7, products_aiv8)
-> do { (asWidgetT GHC.Base.. toWidget)
((blaze-markup-0.7.0.3:Text.Blaze.Internal.preEscapedText
GHC.Base.. Data.Text.pack)
"<div class=\"list-group-item\"><h4 class=\"list-group-item-heading\">");
(asWidgetT GHC.Base.. toWidget)
(toHtml (categoryName category_aiv7));
.... })
rows
我不明白为什么它会期待这个以及我能做些什么来让它发挥作用。非常感谢。
更新: 我的处理程序。
getMenuR :: Handler Html
getMenuR = do
let rows = query
defaultLayout $ do
$(widgetFile "menu")
query = do
cats <- selectList [] [Asc CategoryName]
forM cats $ \(Entity catId cat) -> do
products <- selectList
[ProductCategory ==. catId]
[Asc ProductName]
return (cat, map entityVal products)