英文:
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:
<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)
答案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("index.ejs", {
title:"Home",
infos: infos
})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论