0
我正在使用Express,Node和Pug構建應用程序。在Express中,我有一個端點GET books/:id
。我抓取中間件功能中的所有書籍,然後渲染視圖。在視圖中,我循環了所有書籍,而不是僅顯示一本書的詳細信息,而是顯示所有書籍。我該如何渲染一本書的詳細視圖?如何在帕格而不是所有對象中渲染細節視圖?
下面是詳細信息頁面端點:
// GET the book detail page
router.get('/:id', (req, res, next) => {
bookQuery = Book.findAll({
include: [
{ model: Loan }
],
order: [["title", "DESC"]]
}).then((books) => {
console.log(books);
res.render('book_detail', {
books: books,
// title: loans.Book.title,
// author: loans.Book.author,
// genre: loans.Book.genre,
// first_published: loans.Book.first_published,
// patronFirstName: loans.Patron.first_name,
// patronLastName: loans.Patron.last_name,
// loanedOn: loans.loaned_on,
// return_by: loans.return_by,
// returned_on: loans.returned_on
});
});
});
這裏的觀點:
extends ./layout
block content
body
each book in books
h1 Book: #{book.title}
form
p
label(for='title') Title
input#title(type='text', value=book.title)
p
label(for='author') Author
input#author(type='text', value=book.author)
p
label(for='genre') Genre
input#genre(type='text', value=book.genre)
p
label(for='first_published') First Published
input#first_published(type='text',
value=book.first_published)
p
input(type='submit', value='Update')
h2 Loan History
table
thead
tr
th Book
th Patron
th Loaned on
th Return by
th Returned on
th Action
tbody
tr
td
a(href=`/books/book_detail`)= book.title
td
//- a(href=`/patrons/patron_detail`)=book.Loan.first_name + ' ' + book.Loan.last_name
if book.Loan
a(href='patron_detail.html')
td= book.Loan.loaned_on
td= book.Loan.return_by
td= book.Loan.returned_on
td
a.button(href='return_book.html') Return Book
爲什麼你首先抓住所有的書?是不是有一個原因,你不是通過'id'來抓住一本書? –
我同意@AndrewMyers,但是如果你打算繼續'獲取所有書籍並且只渲染一個',你可能需要在ID上匹配一個有條件的pug,這裏記錄:https://pugjs.org/language /conditionals.html –
@AndrewMyers我在這個項目中使用Sequelize ORM,據我所知我必須查詢該函數中的所有書籍,但我可能是錯的。我不能以某種方式將用戶重定向到所選圖書的書籍詳細信息嗎?但是,我的觀點如何知道? – wellowlbe