gpt4 book ai didi

javascript - 如何在 Pug 中渲染详细 View 而不是其所有对象?

转载 作者:行者123 更新时间:2023-12-03 03:28:45 24 4
gpt4 key购买 nike

我正在使用 Express、Node 和 Pug 构建一个应用程序。在 Express 中,我有一个 GET books/:id 端点。我在中间件函数中获取所有书籍,然后渲染 View 。在 View 中,我循环浏览所有书籍,而不是仅显示一本书的详细信息,而是显示所有书籍。如何只渲染一本书的详细 View ?

这是详细信息页面端点:

// 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

});
});
});

这是 View :

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

最佳答案

这与帕格无关。 Pug 只是渲染数据。

问题是您正在使用Book.findAll()。这得到了一系列书籍。您应该使用 Book.findOne()获得单本书。您还需要指定 where: { id: req.params.id } 来查找具有 URL 中提供的 id 的一本书。

您可能还想将变量 books 更改为 book。并且 View 中的 each book in books 行是不必要的。

Model Usage section of the manual有很多查询的例子。我发现它比上面链接的 API 引用更容易阅读。

关于javascript - 如何在 Pug 中渲染详细 View 而不是其所有对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46187622/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com