Javascript 如何在Pug中渲染细节视图而不是其所有对象?
我正在使用Express、Node和Pug构建一个应用程序。在Express中,我有一个Javascript 如何在Pug中渲染细节视图而不是其所有对象?,javascript,node.js,express,pug,sequelize.js,Javascript,Node.js,Express,Pug,Sequelize.js,我正在使用Express、Node和Pug构建一个应用程序。在Express中,我有一个GET books/:id的端点。我抓取中间件函数中的所有书籍,然后渲染视图。在视图中,我循环浏览所有书籍,而不是只显示一个书籍细节,而是显示所有书籍。如何仅渲染一本书的详细视图 以下是详细信息页面端点: // GET the book detail page router.get('/:id', (req, res, next) => { bookQuery = Book.findAll({
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
这与帕格无关。帕格只是渲染数据 问题是您正在使用
Book.findAll()
。这会得到一系列的书。你应该用它来买一本单曲书。您还需要指定where:{id:req.params.id}
来查找URL中提供了id的一本书
您可能还希望将变量books
更改为book
。视图中的每本书的行都是不必要的
有很多查询的例子。我发现它比我上面链接的API参考资料更容易通读。为什么你一开始就要把所有的书都拿走?有没有理由你不通过id
只抓取一本书?我同意@AndrewMyers,但如果你要继续“获取所有书籍并只渲染一本”,你可能需要在id上匹配一个帕格条件,记录在这里:@AndrewMyers我正在为这个项目使用Sequelize ORM,据我所知,我必须查询该函数中的所有书籍,但我可能错了。我不能将用户重定向到所选书籍的书籍详细信息吗?但是我的视图如何知道这一点呢?@GregSyme我如何获取所选的id并在条件应用程序中使用它?