无法在 Nodejs 中读取未定义的属性(读取 'id')
Cannot read properties of undefined (reading 'id') in Nodejs
您好,我正在尝试借助 node 和 express 制作待办事项应用程序。前端一切正常,但在终端中显示此错误。我已经传递了 todo 对象,尽管它显示 id 未定义。
我在服务器中的表演路线
//Fake Database
let todos = [
{
id: uuid(),
todo: 'Listening to Mozart'
}
]
app.get('/:id', (req, res)=>{
const {id} = req.params;
const todo = todos.find(todo => todo.id == id);
res.render('show', {todo: todo});
我的show.ejs文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title></title>
</head>
<%- include ('_styles.ejs') %>
<%- include ('_new_styles.ejs') %>
<body>
<div class="container">
<div class="card">
<div class="card-body">
<h2>ID:<%= todo.id %> </h2>
<p class="todo-details">
<%= todo.todo %>
</p>
</div>
</div>
<div class="home-btn">
<a href="/"><i class="fas fa-home"></i></a>
</div>
</div>
<div class="blue-ball"></div>
<div class="red-ball"></div>
</body>
</html>
错误是:
TypeError: /home/linux/Desktop/todo/views/show.ejs:16
14| <div class="card">
15| <div class="card-body">
>> 16| <h2>ID:<%= todo.id %> </h2>
17| <p class="todo-details">
18| <%= todo.todo %>
19| </p>
Cannot read properties of undefined (reading 'id')
at eval ("/home/linux/Desktop/todo/views/show.ejs":18:31)
at show (/home/linux/Desktop/todo/node_modules/ejs/lib/ejs.js:692:17)
at tryHandleCache (/home/linux/Desktop/todo/node_modules/ejs/lib/ejs.js:272:36)
at View.exports.renderFile [as engine] (/home/linux/Desktop/todo/node_modules/ejs/lib/ejs.js:489:10)
at View.render (/home/linux/Desktop/todo/node_modules/express/lib/view.js:135:8)
at tryRender (/home/linux/Desktop/todo/node_modules/express/lib/application.js:640:10)
at Function.render (/home/linux/Desktop/todo/node_modules/express/lib/application.js:592:3)
at ServerResponse.render (/home/linux/Desktop/todo/node_modules/express/lib/response.js:1017:7)
at /home/linux/Desktop/todo/server.js:38:9
at Layer.handle [as handle_request] (/home/linux/Desktop/todo/node_modules/express/lib/router/layer.js:95:5)
您确定 todos.find(todo => todo.id == id);
返回任何匹配吗?
您好,我正在尝试借助 node 和 express 制作待办事项应用程序。前端一切正常,但在终端中显示此错误。我已经传递了 todo 对象,尽管它显示 id 未定义。
我在服务器中的表演路线
//Fake Database
let todos = [
{
id: uuid(),
todo: 'Listening to Mozart'
}
]
app.get('/:id', (req, res)=>{
const {id} = req.params;
const todo = todos.find(todo => todo.id == id);
res.render('show', {todo: todo});
我的show.ejs文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title></title>
</head>
<%- include ('_styles.ejs') %>
<%- include ('_new_styles.ejs') %>
<body>
<div class="container">
<div class="card">
<div class="card-body">
<h2>ID:<%= todo.id %> </h2>
<p class="todo-details">
<%= todo.todo %>
</p>
</div>
</div>
<div class="home-btn">
<a href="/"><i class="fas fa-home"></i></a>
</div>
</div>
<div class="blue-ball"></div>
<div class="red-ball"></div>
</body>
</html>
错误是:
TypeError: /home/linux/Desktop/todo/views/show.ejs:16
14| <div class="card">
15| <div class="card-body">
>> 16| <h2>ID:<%= todo.id %> </h2>
17| <p class="todo-details">
18| <%= todo.todo %>
19| </p>
Cannot read properties of undefined (reading 'id')
at eval ("/home/linux/Desktop/todo/views/show.ejs":18:31)
at show (/home/linux/Desktop/todo/node_modules/ejs/lib/ejs.js:692:17)
at tryHandleCache (/home/linux/Desktop/todo/node_modules/ejs/lib/ejs.js:272:36)
at View.exports.renderFile [as engine] (/home/linux/Desktop/todo/node_modules/ejs/lib/ejs.js:489:10)
at View.render (/home/linux/Desktop/todo/node_modules/express/lib/view.js:135:8)
at tryRender (/home/linux/Desktop/todo/node_modules/express/lib/application.js:640:10)
at Function.render (/home/linux/Desktop/todo/node_modules/express/lib/application.js:592:3)
at ServerResponse.render (/home/linux/Desktop/todo/node_modules/express/lib/response.js:1017:7)
at /home/linux/Desktop/todo/server.js:38:9
at Layer.handle [as handle_request] (/home/linux/Desktop/todo/node_modules/express/lib/router/layer.js:95:5)
您确定 todos.find(todo => todo.id == id);
返回任何匹配吗?