问题描述
嗨,我是 Haskell 和 Yesod 的初学者,我需要帮助解决这个问题。
这些是我的实体:
Location
name Text sqltype=varchar(255)
address AddressId Maybe sqltype=varchar(255)
UniqueLocationName name
deriving Show Typeable
Manifestation
name Text sqltype=varchar(255)
description Text Maybe sqltype=varchar(255)
category Category Maybe
startDateTime UTCTime sqltype=DateTime
location LocationId Maybe sqltype=varchar(255)
UniqueManName name
deriving Show Typeable
这是我调用 man-details 模板的处理程序:
getManDetailsR :: ManifestationId -> Handler Html
getManDetailsR mid = do
(_,user) <- requireAuthPair
md <- runDB $ get404 mid -- type is Manifestation
defaultLayout $ do
setTitle "Manifestation details"
$(widgetFile "man-details")
以及我想显示事件信息的 man-details hamlet 文件的一部分:
<div class="row">
<div class="col-md-5">
<div class="project-info-Box mt-0">
<h2>#{manifestationName md}
<p class="mb-0">#{fromJust(manifestationDescription md)}
<div class="project-info-Box">
<p><b>Start :</b>{show $ manifestationStartDateTime md}
<p><b>Location :</b>#{locationName (manifestationLocation md)}
在这种情况下错误是: 无法匹配预期的类型“位置” 实际类型为“也许(关键位置)”
然后我尝试这样:
$maybe l <- manifestationLocation md
<p><b>Location :</b>{locationName l}
错误是: 无法匹配预期的类型“位置” 实际类型为“关键位置”
对于这个大问题,我深表歉意,但我不知道如何解决这个问题,即如何仅从这对(关键位置)中获取值?
欢迎任何建议和帮助,
谢谢。
解决方法
我会发布我的解决方案,这是初学者的问题,但它可能对某人有用。
正如@Willem Van Onsem 在评论中所写,我再次对数据库进行了查询
现在寻找位置。
现在处理程序看起来像这样,由于使用了 Maybe 包装器,所以使用 fromJust 函数:
getManDetailsR :: ManifestationId -> Handler Html
getManDetailsR mid = do
(_,user) <- requireAuthPair
md <- runDB $ get404 mid
loc <- runDB $ get404 $ fromJust(manifestationLocation md) -- and now use loc in template
liftIO $ print (loc)
defaultLayout $ do
setTitle "Manifestation details"
$(widgetFile "man-details")