我有数据Book和Author,而Books有一个外键AuthorId。
我想列出DB的书籍。我需要作者的名字,但书籍实体只有作者的ID,为什么我使用get
函数来获取作者数据,但在哈姆雷特文件中我无法得到作者的名字,因为get
函数返回也许是作者。
让我看看我的代码:
Book
isbn Text
title Text
description Textarea Maybe
author AuthorId
UniqueBook isbn
Author
name Text
UniqueAuthor name
请求的get函数:
getBookListR :: Handler Html
getBookListR = do
books <- runDB $ selectList [BookTitle !=. ""] []
defaultLayout $ do
$(widgetFile "booklistpage")
booklistpage hamlet文件内容:
$if not $ null books
<table .table>
$forall Entity bookId book <- books
<tr>
<th th rowspan="4">Image
<td> #{bookTitle book}
<tr>
<td>
$maybe author <- get (bookAuthor book)
#{authorName author}
<tr>
<td> #{bookIsbn book}
<tr>
<td>
$maybe description <- bookDescription book
#{description}
这部分代码有问题
<td>
$maybe author <- get (bookAuthor book)
#{authorName author}
我收到了这个错误:
Handler\BookList.hs:9:19:
Couldn't match expected type `Author'
with actual type `Maybe Author'
In the first argument of `authorName', namely `author_afBtA'
In the first argument of `toHtml', namely `authorName author_afBtA'
In the first argument of `asWidgetT . toWidget', namely
`toHtml (authorName author_afBtA)'
我认为$也许会帮助我,但也许我误解了这个概念。我想理解为什么这段代码不起作用,以及当我们在hashlet文件中迭代时只有键时,这种情况的解决方案是什么。