EJS 文件中未定义 Express 变量、ReferenceError
我正在关注 Express 的开发教程(他的代码问题在 31:20 左右出现)
尽管完全按照教程进行操作,但还是存在一些错误。这是我的路线:
(\VSCODE\testing\routes) //authors.js:
router.get('/', async (req,res) => {
if (req.query.name != null && req.query.name != ''){
searchOptions = { name: RegExp(req.query.name, 'i')}
}
try {
let searchOptions = {}
const authors = await Author.find(searchOptions)
res.render('authors/index', {
authors: authors,
searchOptions: req.query
})
} catch (err)
{
console.log(err)
res.redirect('/')
}
})
这是生成错误的标记/模板文件: (\VSCODE\testing\views\authors) //index.ejs
<h2> Search Authors </h2>
<form action="/authors" method = "GET">
<label> Name </label>
<input type = "text" name ="name" value="<%= searchOptions.name %>">
<button type = "submit"> Search </button>
</form>
<br>
<% authors.forEach(author => { %>
<div><%= author.name %></div>
<% }) %>
错误:
ReferenceError: C:\Users\matth\VSCODE\testing\views\authors\index.ejs:4
2| <form action="/authors" method = "GET">
3| <label> Name </label>
>> 4| <input type = "text" name ="name" value="<%= searchOptions.name %>">
5| <button type = "submit"> Search </button>
6| </form>
7| <br>
searchOptions is not defined
at eval ("C:\\Users\\matth\\VSCODE\\testing\\views\\authors\\index.ejs":12:26)
at index (C:\Users\matth\VSCODE\testing\node_modules\ejs\lib\ejs.js:692:17)
at tryHandleCache (C:\Users\matth\VSCODE\testing\node_modules\ejs\lib\ejs.js:272:36)
at View.exports.renderFile [as engine] (C:\Users\matth\VSCODE\testing\node_modules\ejs\lib\ejs.js:489:10)
at View.render (C:\Users\matth\VSCODE\testing\node_modules\express\lib\view.js:135:8)
at tryRender (C:\Users\matth\VSCODE\testing\node_modules\express\lib\application.js:640:10)
at Function.render (C:\Users\matth\VSCODE\testing\node_modules\express\lib\application.js:592:3)
at ServerResponse.render (C:\Users\matth\VSCODE\testing\node_modules\express\lib\response.js:1017:7)
at ServerResponse.res.render (C:\Users\matth\VSCODE\testing\node_modules\express-ejs-layouts\lib\express-layouts.js:77:18)
at C:\Users\matth\VSCODE\testing\routes\authors.js:34:9
I am following a development tutorial with Express (problem with his code comes up around 31:20)
There's some errors despite following the tutorial exactly. Here's my route:
(\VSCODE\testing\routes)
//authors.js:
router.get('/', async (req,res) => {
if (req.query.name != null && req.query.name != ''){
searchOptions = { name: RegExp(req.query.name, 'i')}
}
try {
let searchOptions = {}
const authors = await Author.find(searchOptions)
res.render('authors/index', {
authors: authors,
searchOptions: req.query
})
} catch (err)
{
console.log(err)
res.redirect('/')
}
})
Here is my Markup/Template file that is generating the error:
(\VSCODE\testing\views\authors)
//index.ejs
<h2> Search Authors </h2>
<form action="/authors" method = "GET">
<label> Name </label>
<input type = "text" name ="name" value="<%= searchOptions.name %>">
<button type = "submit"> Search </button>
</form>
<br>
<% authors.forEach(author => { %>
<div><%= author.name %></div>
<% }) %>
The error:
ReferenceError: C:\Users\matth\VSCODE\testing\views\authors\index.ejs:4
2| <form action="/authors" method = "GET">
3| <label> Name </label>
>> 4| <input type = "text" name ="name" value="<%= searchOptions.name %>">
5| <button type = "submit"> Search </button>
6| </form>
7| <br>
searchOptions is not defined
at eval ("C:\\Users\\matth\\VSCODE\\testing\\views\\authors\\index.ejs":12:26)
at index (C:\Users\matth\VSCODE\testing\node_modules\ejs\lib\ejs.js:692:17)
at tryHandleCache (C:\Users\matth\VSCODE\testing\node_modules\ejs\lib\ejs.js:272:36)
at View.exports.renderFile [as engine] (C:\Users\matth\VSCODE\testing\node_modules\ejs\lib\ejs.js:489:10)
at View.render (C:\Users\matth\VSCODE\testing\node_modules\express\lib\view.js:135:8)
at tryRender (C:\Users\matth\VSCODE\testing\node_modules\express\lib\application.js:640:10)
at Function.render (C:\Users\matth\VSCODE\testing\node_modules\express\lib\application.js:592:3)
at ServerResponse.render (C:\Users\matth\VSCODE\testing\node_modules\express\lib\response.js:1017:7)
at ServerResponse.res.render (C:\Users\matth\VSCODE\testing\node_modules\express-ejs-layouts\lib\express-layouts.js:77:18)
at C:\Users\matth\VSCODE\testing\routes\authors.js:34:9
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试添加 error 属性并在
render
方法中将其设置为error: false
。像这样,
Try adding the error property and set it as
error: false
in yourrender
method.Like this,