我正在与Yesod合作,我正在尝试打印一个包含事件列表及其所属城市的表格。
我有以下型号:
City
name Text
countryId CountryId
UniqueCity name
deriving Eq
deriving Show
Event
title Text
description Text
date UTCTime
cityId CityId
deriving Eq
deriving Show`
以及处理程序中的以下代码:
`module Handler.Event where
import Import
import Yesod.Form.Bootstrap3
import Database.Persist.Sql
getEventR :: Handler Html
getEventR = do
events <- runDB $ selectList [] [Asc EventTitle]
defaultLayout $ do
setTitle "Events"
[whamlet|
<h1>Events
<table>
<thead>
<tr>
<th>Event title</th>
<th>Event date</th>
<th>Event city</th>
<tbody>
$forall Entity eventId event <- events
<tr>
<td>#{eventTitle event}
<td>#{show (eventDate event)}
<td>#{addCity eventId}
|]
addCity event = do
city <- getCity $ eventCityId event
return city
getCity cid = do
city <- runDB $ get404 cid
return city`
但是当我尝试运行它时,我收到以下错误: 无法将预期类型“事件”与实际类型“关键事件”匹配 在'addCity'的第一个参数中,即'eventId_ajRi' 在'toHtml'的第一个参数中,即'addCity eventId_ajRi'
我想我理解错误,但我真的不知道如何解决它以及如何显示城市名称。有人可以告诉我应该怎么做?
我也尝试过这样做,但仍然没有运气:
module Handler.Event where
import Import
import Yesod.Form.Bootstrap3
import Database.Persist.Sql
getEventR :: Handler Html
getEventR = do
events <- runDB $ selectList [] [Asc EventTitle]
defaultLayout $ do
setTitle "Events"
[whamlet|
<h1>Events
<table>
<thead>
<tr>
<th>Event name</th>
<th>Event date</th>
<th>Event city</th>
<ul>
<tbody>
$forall ev <- map entityVal events
<tr>
<td>#{eventTitle ev}
<td>#{show (eventDate ev)}
<td>#{addCity ev}
|]
addCity ev = do
eve <- entityVal ev
city <- getCity $ eventCityId eve
return city
getCity cid = do
city <- runDB $ get404 cid
return $ cityName city
通过这些修改,我现在得到:
Couldn't match expected type 'Entity (HandlerT site0 IO Event)' with actual type 'Event'
In the first argument of 'addCity', namely 'ev_ajRc'
In the first argument of 'toHtml', namely 'addCity ev_ajRc'