ReferenceError: 在连接我的前端与我的Node.js服务器时出错

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

ReferenceError: Error in connecting my frontend to my server in Nodejs

问题

以下是您要翻译的内容:

"Good day geniuses in the house.

Please I am new in web dev. I was trying to build a simple project using nodejs/expressjs. I encountered a problem connecting my frontend to my server.

This is my frontend code:

<div class="blog content">
    <h2>All Blogs</h2>

    <% if (infos.length > 0) { %>
        <% infos.forEach((info)=>{ %>
            <div class="post-content">
                <div>
                    <a href="">

                        <h3 class="title"><%=info.title %></h3>
                        <p class="snippet"><%= info.snippet %></p>
                        <br> <br>
                    </a>
                </div>
             </div>

      <%  }) %>

       <%  } else {%>
        <p>No post today</p>
     <%  } %>
</div>

and below is my server code that i created;

const express = require("express")
const app = express();

app.set("views engine", "ejs")

 app.use(express.static("public"))
app.use(express.urlencoded())

app.get("/",  (req, res) => {
    const infos = [
        {
            title : "Liverpool owns Dominik",
            snippet : "Dominik is a liverpool player"
        },

        {
            title : "Arsenal owns Declan Rice",
            snippet : "Rice came with Harvetz to partey"
        },
     ]

    res.render("index.ejs", {title:"Home"})
})

but this is the error result i do get;

ReferenceError: C:\Users\go\Desktop\aid\views\index.ejs:8
    6|         <h2>All Blogs</h2>
    7|
 >> 8|         <% if (infos.length > 0) { %>
    9|             <% infos.forEach((info)=>{ %>
    10|                 <div class="post-content">
    11|                     <div>

infos is not defined
    at eval ("C:\Users\go\Desktop\aid\views\index.ejs":16:8)
    at index (C:\Users\go\Desktop\aid\node_modules\ejs\lib\ejs.js:703:17)
    at tryHandleCache (C:\Users\go\Desktop\aid\node_modules\ejs\lib\ejs.js:274:36)
    at View.exports.renderFile [as engine] (C:\Users\go\Desktop\aid\node_modules\ejs\lib\ejs.js:491:10)
    at View.render (C:\Users\go\Desktop\aid\node_modules\express\lib\view.js:135:8)
    at tryRender (C:\Users\go\Desktop\aid\node_modules\express\lib\application.js:657:10)
    at Function.render (C:\Users\go\Desktop\aid\node_modules\express\lib\application.js:609:3)
    at ServerResponse.render (C:\Users\go\Desktop\aid\node_modules\express\lib\response.js:1039:7)
    at C:\Users\go\Desktop\aid\app.js:32:9
    at Layer.handle [as handle_request] (C:\Users\go\Desktop\aid\node_modules\express\lib\router\layer.js:95:5)

请注意,我没有翻译代码部分,只翻译了您的文字说明和错误信息。

英文:

Good day geniuses in the house.

Please I am new in web dev. I was trying to build a simple project using nodejs/expressjs. I encountered a problem connecting my frontend to my server.

This is my frontend code:

&lt;div class=&quot;blog content&quot;&gt;
        &lt;h2&gt;All Blogs&lt;/h2&gt;

        &lt;% if (infos.length &gt; 0) { %&gt;
            &lt;% infos.forEach((info)=&gt;{ %&gt;
                &lt;div class=&quot;post-content&quot;&gt;
                    &lt;div&gt;
                        &lt;a href=&quot;&quot;&gt;

                             &lt;h3 class=&quot;title&quot;&gt;&lt;%=info.title %&gt;  &lt;/h3&gt;
                            &lt;p class=&quot;snippet&quot;&gt;&lt;%= info.snippet %&gt;&lt;/p&gt;
                            &lt;br&gt; &lt;br&gt;
                         &lt;/a&gt;
                    &lt;/div&gt;
                 &lt;/div&gt;

          &lt;%  }) %&gt; 

           &lt;%  } else {%&gt;
            &lt;p&gt;No post today&lt;/p&gt;
         &lt;%  } %&gt; 
    &lt;/div&gt; 

and below is my server code that i created;

const express = require(&quot;express&quot;)
const app = express();


app.set(&quot;views engine&quot;, &quot;ejs&quot;)


 app.use(express.static(&quot;public&quot;))
app.use(express.urlencoded())

app.get(&quot;/&quot;,  (req, res) =&gt; {
    const infos = [
        {
            title : &quot;Liverpool owns Dominik&quot;,
            snippet : &quot;Dominik is a liverpool player&quot;
        },

        {
            title : &quot;Arsenal owns Declan Rice&quot;,
            snippet : &quot;Rice came with Harvetz to partey&quot;
        },
     ]

    res.render(&quot;index.ejs&quot;, {title:&quot;Home&quot;})
})

but this is the error result i do get;

ReferenceError: C:\Users\go\Desktop\aid\views\index.ejs:8
    6|         &lt;h2&gt;All Blogs&lt;/h2&gt;
    7|
 &gt;&gt; 8|         &lt;% if (infos.length &gt; 0) { %&gt;
    9|             &lt;% infos.forEach((info)=&gt;{ %&gt;
    10|                 &lt;div class=&quot;post-content&quot;&gt;
    11|                     &lt;div&gt;

infos is not defined
    at eval (&quot;C:\\Users\\go\\Desktop\\aid\\views\\index.ejs&quot;:16:8)
    at index (C:\Users\go\Desktop\aid\node_modules\ejs\lib\ejs.js:703:17)
    at tryHandleCache (C:\Users\go\Desktop\aid\node_modules\ejs\lib\ejs.js:274:36)
    at View.exports.renderFile [as engine] (C:\Users\go\Desktop\aid\node_modules\ejs\lib\ejs.js:491:10)
    at View.render (C:\Users\go\Desktop\aid\node_modules\express\lib\view.js:135:8)
    at tryRender (C:\Users\go\Desktop\aid\node_modules\express\lib\application.js:657:10)
    at Function.render (C:\Users\go\Desktop\aid\node_modules\express\lib\application.js:609:3)
    at ServerResponse.render (C:\Users\go\Desktop\aid\node_modules\express\lib\response.js:1039:7)
    at C:\Users\go\Desktop\aid\app.js:32:9
    at Layer.handle [as handle_request] (C:\Users\go\Desktop\aid\node_modules\express\lib\router\layer.js:95:5)

答案1

得分: 2

The res.render方法“接受一个可选参数,其中包含视图的本地变量”。您需要将infos传递给您的视图。

res.render("index.ejs", {
   title: "Home",
   infos: infos
})
英文:

The res.render method "accepts an optional parameter that is an object containing local variables for the view". You need to pass infos to your views.

res.render(&quot;index.ejs&quot;, {
   title:&quot;Home&quot;,
   infos: infos
})

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

发表评论

匿名网友

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

确定