英文:
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('/home', (req, res) => {
const articles = [{
title: 'test article',
subtitle: 'test subtitle',
createdAt: new Date(),
lastModified: new Date()
}]
res.render('author', {author:articles})
})
router.get('/draft', (req, res) => {
res.render('draft', {author:articles})
})
app.set('view engine', 'ejs');
module.exports=router
here is my author.js file
<!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>
and here is my error message
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
答案1
得分: 1
你使用 res.render('author', {author:articles})
来发送数据。
所以,你需要使用
<% author.forEach(article => { %>
而不是
<% articles.forEach(article => { %>
或者如果要继续使用 articles
,你需要将
res.render('author', {author:articles})
改为
res.render('author', {articles:articles})
任何一种方式都应该使其正常工作。
英文:
You use res.render('author', {author:articles})
to send the data.
So, you need to use
<% author.forEach(article => { %>
instead of
<% articles.forEach(article => { %>
Or to be able to use articles
you have to change
res.render('author', {author:articles})
to
res.render('author', {articles:articles})
Either one should make it work properly.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论