EJS 文件中未定义 Express 变量、ReferenceError

发布于 2025-01-10 09:05:02 字数 2886 浏览 0 评论 0原文

我正在关注 Express 的开发教程(他的代码问题在 31:20 左右出现)

https://www.youtube.com/watch?v=esy4nRuShl8&list=PLZlA0Gpn_vH8jbFkBjOuFjhxANC63OmXM&index=7&ab_channel=WebDevSimplified

尽管完全按照教程进行操作,但还是存在一些错误。这是我的路线:

(\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)

https://www.youtube.com/watch?v=esy4nRuShl8&list=PLZlA0Gpn_vH8jbFkBjOuFjhxANC63OmXM&index=7&ab_channel=WebDevSimplified

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

°如果伤别离去 2025-01-17 09:05:02

尝试添加 error 属性并在 render 方法中将其设置为 error: false

像这样,

res.render('authors/index', {  
   authors: authors,
   searchOptions: req.query,
   error: false
})

Try adding the error property and set it as error: false in your render method.

Like this,

res.render('authors/index', {  
   authors: authors,
   searchOptions: req.query,
   error: false
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文