我是在线JavaScript技术学位课程的学生,所以显然我是新手。我正在使用express,pug和sequelize构建应用程序。在我的应用程序的第一个版本中,我有第一个代码块,它带回了一个Patrons列表。它运作得很好但是基本列表没有搜索功能,也没有分页功能。这是代码:
router.get('/', (req, res, next) => {
Patron.findAll({
order: [
['last_name', 'ASC'],
['first_name', 'ASC']
]
}).then(patrons => {
const columns = [
"Name",
"Address",
"Email",
"Library ID",
"Zip"
];
const patronData = patrons.map(patron => {
return patron.get({
plain: true
});
});
const title = 'Patrons';
res.render('all', { patronData, columns, title, content });
});
});
现在我正在尝试添加搜索功能和分页,我修改了我的代码,如下所示:
router.get('/', (req, res, next) => {
// no page selected - so page = 1
if (req.query.page === undefined) {
res.redirect(
'/patron?page=1'
);
}
let workingQuery;
// set search flag to true or false
search = req.query.search ? req.query.search : false;
// a query with no search parameters
if ( req.query.search === undefined ) {
workingQuery = Patron.findAndCountAll({
order: [
['last_name', 'ASC'],
['first_name', 'ASC']
],
offset: (req.query.page * 10) - 10,
limit: 10
});
}
// query for patrons with search parameters (for pages other than page 1, page 1 handled in the POST above)
if (req.query.search) {
workingQuery = Patron.findAndCountAll({
where: {
last_name: {
$like: `%${ req.body.last_name.toLowerCase() }%`,
},
first_name: {
$like: `%${ req.body.first_name.toLowerCase() }%`,
},
library_id: {
$like: `%${ req.body.library_id.toLowerCase() }%`,
}
},
offset: (req.query.page * 10) - 10,
limit: 10
});
}
// render the patron query
workingQuery.then(patrons => {
currPage = req.query.page;
const columns = [
"Name",
"Address",
"Email",
"Library ID",
"Zip"
];
const patronData = patrons.map(patron => {
return patron.get({
plain: true
});
});
const title = 'Patrons';
res.render('list_selector', {
entity,
title,
filter,
columns,
patronData
});
}).catch(error => {
res.status(500).send(error);
});
});
现在这是我的视口窗口中显示的内容:
{}
在控制台中返回此错误:
Failed to load resource: the server responded with a status of 500 (Internal Server Error)
我将包含我的相关pug文件(以防他们帮助):
body
#top.header
a.top(href='/#top')
img.logo(src='/static/images/librarian.png', alt='librarian')
h1.app-title Welcome to Library Manager
h3.head-books Books
h3.head-patrons Patrons
h3.head-loans Loans
a.book-new.nav(href='/book/new') New Book
a.book-list-all.nav(href='/book?page=1') List All
a.book-list-co.nav(href='/book?page=1&filter=checked_out') List Checked Out
a.book-list-od.nav(href='/book?page=1&filter=overdue') List Overdue
a.patron-new.nav(href='/patron/new') New Patron
a.patron-list-all.nav(href='/patron?page=1') List All
a.loan-new.nav(href='/loan/new') New Loan
a.loan-list-all.nav(href='/loan?page=1') List All
a.loan-list-co.nav(href='/loan?page=1&filter=checked_out') List Checked Out
a.loan-list-od.nav(href='/loan?page=1&filter=overdue') List Overdue
.gap
和选择器哈巴狗
extends ./layout
block content
include ./includes/_head_nav
.card
h2=`${title} - ${filter}`
//if entity === "book"
// include ./includes/_book_search
//if entity === "patron"
// include ./includes/_patron_search
table
include ./includes/_table_heads
tbody
if entity === "book"
include ./includes/_table_rows_books
if entity === "patron"
include ./includes/_table_rows_patrons
if entity === "loan"
include ./includes/_table_rows_loans
include ./includes/_pagination
任何帮助将不胜感激我已经梳理了Express文档,Sequelize文档甚至尝试在VS Code中学习一些更好的调试技巧,但我完全死在了水中。