我在我的Node应用程序中遇到了一个引用错误。

huangapple go评论83阅读模式
英文:

I am getting a reference error in my node application

问题

这是我的 author.ejs 文件,代码部分不需要翻译:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Blog</title>
</head>
<body>
  <div class="container">
    <h1 class="mb-4">Blog Articles</h1>
    <a href="/author/draft" class="btn btn-success">New Draft</a>

    <% articles.forEach(article => { %>
      <div class="card mt-4">
        <div class="card-body">
          <h4 class="card-title"><%= article.title %></h4>
          <div class="card-subtitle text-muted mb-2">
            <%= article.createdAt.toLocaleDateString() %>
          </div>
          <div class="card-text mb-2"><%= article.description %></div>
          <!-- <a href="author/<%= article.slug %>" class="btn btn-primary">Read More</a>
          <a href="author/edit/<%= article.id %>" class="btn btn-info">Edit</a>
          <form action="/author/<%= article.id %>?_method=DELETE" method="POST" class="d-inline"> -->
            <button type="submit" class="btn btn-danger">Delete</button>
          </form>
        </div>
      </div>
    <% }) %>
  </div>
</body>
</html>

你的错误信息是:

ReferenceError: C:\Users\sukho\Desktop\Databasetemplate\dnw-coursework-template-main\views\author.ejs:32
    30|     <a href="/author/draft" class="btn btn-success">New Draft</a>

    31| 

 >> 32|     <% articles.forEach(article => { %>

    33|       <div class="card mt-4">

    34|         <div class="card-body">

    35|           <h4 class="card-title"><%= article.title %></h4>


articles is not defined

错误信息指出 articles 未定义,这可能是因为在你的 author.ejs 文件中,articles 并未传递给模板。确保在渲染模板时传递 articles,以便在模板中使用它。你可以在 router.get('/home', ...) 处检查一下是否正确传递了 articles,并确保在其他需要使用它的路由中也传递了它。

英文:

I'm new to express.js and node and am currently getting a reference error in my code, please do advise on how I should solve this? apparently the code cant find my reference to the "articles" constatn all though its defined.

Here is my author.ejs file

const router = express.Router()

router.get(&#39;/home&#39;, (req, res) =&gt; {
    const articles = [{
        title: &#39;test article&#39;,
        subtitle: &#39;test subtitle&#39;,
        createdAt: new Date(),
        lastModified: new Date()
    }]
    res.render(&#39;author&#39;, {author:articles})
})

router.get(&#39;/draft&#39;, (req, res) =&gt; {
   
    res.render(&#39;draft&#39;, {author:articles})
})
app.set(&#39;view engine&#39;, &#39;ejs&#39;);

module.exports=router

here is my author.js file

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
  &lt;meta charset=&quot;UTF-8&quot;&gt;
  &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
  &lt;link rel=&quot;stylesheet&quot; href=&quot;https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css&quot; integrity=&quot;sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm&quot; crossorigin=&quot;anonymous&quot;&gt;
  &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;ie=edge&quot;&gt;
  &lt;title&gt;Blog&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
  &lt;div class=&quot;container&quot;&gt;
    &lt;h1 class=&quot;mb-4&quot;&gt;Blog Articles&lt;/h1&gt;
    &lt;a href=&quot;/author/draft&quot; class=&quot;btn btn-success&quot;&gt;New Draft&lt;/a&gt;

    &lt;% articles.forEach(article =&gt; { %&gt;
      &lt;div class=&quot;card mt-4&quot;&gt;
        &lt;div class=&quot;card-body&quot;&gt;
          &lt;h4 class=&quot;card-title&quot;&gt;&lt;%= article.title %&gt;&lt;/h4&gt;
          &lt;div class=&quot;card-subtitle text-muted mb-2&quot;&gt;
            &lt;%= article.createdAt.toLocaleDateString() %&gt;
          &lt;/div&gt;
          &lt;div class=&quot;card-text mb-2&quot;&gt;&lt;%= article.description %&gt;&lt;/div&gt;
          &lt;!--&lt;a href=&quot;author/&lt;%= article.slug %&gt;&quot; class=&quot;btn btn-primary&quot;&gt;Read More&lt;/a&gt;
          &lt;a href=&quot;author/edit/&lt;%= article.id %&gt;&quot; class=&quot;btn btn-info&quot;&gt;Edit&lt;/a&gt;
          &lt;form action=&quot;/author/&lt;%= article.id %&gt;?_method=DELETE&quot; method=&quot;POST&quot; class=&quot;d-inline&quot;&gt;--&gt;
            &lt;button type=&quot;submit&quot; class=&quot;btn btn-danger&quot;&gt;Delete&lt;/button&gt;
          &lt;/form&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;% }) %&gt;
  &lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;

and here is my error message

ReferenceError: C:\Users\sukho\Desktop\Databasetemplate\dnw-coursework-template-main\views\author.ejs:32
    30|     &lt;a href=&quot;/author/draft&quot; class=&quot;btn btn-success&quot;&gt;New Draft&lt;/a&gt;

    31| 

 &gt;&gt; 32|     &lt;% articles.forEach(article =&gt; { %&gt;

    33|       &lt;div class=&quot;card mt-4&quot;&gt;

    34|         &lt;div class=&quot;card-body&quot;&gt;

    35|           &lt;h4 class=&quot;card-title&quot;&gt;&lt;%= article.title %&gt;&lt;/h4&gt;


articles is not defined

答案1

得分: 1

你使用 res.render('author', {author:articles}) 来发送数据。

所以,你需要使用

&lt;% author.forEach(article =&gt; { %&gt;

而不是

&lt;% articles.forEach(article =&gt; { %&gt;

或者如果要继续使用 articles,你需要将

res.render('author', {author:articles})

改为

res.render('author', {articles:articles})

任何一种方式都应该使其正常工作。

英文:

You use res.render(&#39;author&#39;, {author:articles}) to send the data.

So, you need to use

&lt;% author.forEach(article =&gt; { %&gt; 

instead of

&lt;% articles.forEach(article =&gt; { %&gt;

Or to be able to use articles you have to change

res.render(&#39;author&#39;, {author:articles})

to

res.render(&#39;author&#39;, {articles:articles})

Either one should make it work properly.

huangapple
  • 本文由 发表于 2023年7月20日 09:37:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76726149.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定