for all 循环中的 Yesod Hamlet 类型错误令人困惑
Yesod Hamlet type error in for all loop is confusing
我有一个返回类型列表的 Yesod 处理程序 [(Category, [Product])] 我正试图在我的小村庄模板中遍历它。
$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)
在 Michael 的帮助下,只需将 let rows = query
替换为 rows <- runDB query
即可
我有一个返回类型列表的 Yesod 处理程序 [(Category, [Product])] 我正试图在我的小村庄模板中遍历它。
$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)
在 Michael 的帮助下,只需将 let rows = query
替换为 rows <- runDB query