我刚刚开始使用 Spock、persistent 和 blaze-html 进行 Haskell Web 开发。
在我拥有的一条路线中,我想加载我选择的表中的每一行。我做这样的事情:
get ("/show/flight/" <//> (var :: Var Integer)) $ \f -> requireUser $ \(_, l) -> do
fs <- runSQL $ loadFlightInfos f
case fs of
[] -> blaze $ template False (showResultAlertBar False "Oops, something went wrong! Please try again.")
_ -> blaze $ template True (H.toHtml $ usersUsername l) loadFlightSeat
where
loadFlightSeat :: H.Html
loadFlightSeat =
forM_ fs $ \fs' -> do
sid <- runSQL $ getSeatIdByFlight fs' c
case sid of
Nothing -> H.div H.! A.class_ "alert alert-danger" $ "Oops, something went wrong! Please try again."
Just rid -> H.a H.! A.href (H.toValue $ "/flight/seat/" <> show c <> "/" <> show (fromIntegral $ (fromSqlKey . entityKey) sid)) H.! A.class_ "btn btn-theme" $ H.toHtml fs'
loadFlightInfos
有类型:
Integer -> SqlPersistM [Entity Flight]
和 getSeatIdByFlight:
T.Text -> Integer -> SqlPersistM (Maybe (Entity Flight))
我从 Spock 的博客示例应用程序中复制runSQL
,它是这样的:
runSQL :: (HasSpock m, SpockConn m ~ SqlBackend) => SqlPersistT (NoLoggingT (ResourceT IO)) a -> m a
runSQL action = runQuery $ \conn -> runResourceT $ runNoLoggingT $ runSqlConn action conn
我得到的类型错误:
Couldn't match expected type ‘SqlBackend’
with actual type ‘SpockConn Text.Blaze.Internal.MarkupM’
In the expression: runSQL
In a stmt of a 'do' block:
sid <- runSQL $ getSeatIdByFlight fs' c
我仍然不明白这种类型错误,因为我知道runSQL
它是从持久化到 Spock 的包装器,如果我只是想输出 HTML,为什么它不能通过类型检查?
如何解决此类型错误?