node.js - NodeJS Express 和 EJS 使用 id 渲染路由
问题描述
我是否必须使用 id 才能在我的 ejs 文件中使用不同的对象?当我尝试使用与我的 json 对象不同的属性进行渲染时,我得到一个“未定义的错误”。
这是我的 JSON:
[{
"id": 0,
"TABLE_NAME": "t_Cards",
"NUM_OF_ROWS": 33
},
{
"id":1,
"TABLE_NAME":"t_Users",
"NUM_OF_ROWS": 44
},
{
"id":2,
"TABLE_NAME":"t_Date",
"NUM_OF_ROWS": 44
}]
这是我的代码:
const express = require('express');
const app = express();
const dataTables = require('./routes/data/dataTables.json');
const tableRouter = express.Router();
app.set('view engine', 'ejs');
app.set('views', './views');
// This is going to render the index page.
app.get('/', (req, res) => {
res.render('index', {
dataTables,
});
app.use('/dataDictionarys', tableRouter);
// I think the /:id might be part of the Express Framework? I could be wrong
tableRouter.route('/:id').get((req, res) => {
const ID = req.params.id;
res.render('./dataDictionarys/dataDictionary', {
dataTable : dataTables[ID],
})
});
console.log("app is listening");
app.listen(5000);
我必须使用id吗?当我尝试使用 TABLE_NAME 来识别我的对象并在 URL 中使用 TABLE_NAME 时,它显示 TABLE_NAME 未定义。id 只是 Express 或 ejs 框架的一部分吗?这是否意味着我的所有 json 对象都具有 id 属性是最佳实践?
解决方案
推荐阅读
- javascript - 如何访问嵌套集合 Firestore React
- javascript - 为什么这种冒泡排序会以错误的顺序留下 2 个元素?
- r - R:前向概率自定义函数导致舍入误差
- javascript - 关于如何在 jscript for 循环中捕获最后一个索引的想法,其中索引的长度是未知的?
- python - Python Page Rank Streaming Application using Hadoop, py4j.protocol.Py4JJavaError: An error occurred while calling o27.partitions
- angular - Angular 10 不再通过拦截器向请求添加标头
- java - 如何在 MacOS 上的 Java 游戏中包含资源?
- c# - 如何在 Blazor @inject ClassName classObject 中的“普通”c# 类中使用/注入服务
- node.js - 生产环境中客户端的socket.io错误
- c# - Bootstrap Modal 显示空白编辑数据