语法错误:意外的标记 '{'编译 ejs 时在 __dirname\views\admin\users.ejs 中
我目前正在开发一个带有 CRUD 操作的 Nodejs 应用程序,用于管理端。这是我的表格,管理员可以编辑所有用户。由于某种原因,我在 ejs 中收到了 Unexpected token '{' 的错误,但我没有看到任何未闭合的括号或类似内容。我很困惑。如果您在这里看到问题,请告诉我。我开始认为该错误与我的观点无关,更与我的代码无关。尽管我仍然没有在代码中发现任何问题,但新的眼光有望解决我的问题。
我的 ejs 文件:
<div class="card">
<div class="card-header">
<div class="row">
<div class="col-8">
<h4 class="font-weight-bold">Users</h4>
</div>
</div>
</div>
<div class="card-body">
<table class="table table-hover">
<thead class="table-dark">
<tr>
<th scope="col">Name</th>
<th scope="col">Role</th>
<th scope="col">Content Creator</th>
<th scope="col">Email</th>
<th scope="col">Username</th>
</tr>
</thead>
<tbody>
<% if(users.length> 0 { %>
<% for (let user of users) { %>
<tr>
<th scope="row">
<%= user.name %>
</th>
<td>
<%= user.role %>
</td>
<td>
<%= user.platform %>
</td>
<td>
<%= user.email %>
</td>
<td>
<%= user.username %>
</td>
<td>
<a href="admin/update/<%= user._id %>" class="" btn btn-sm btn-warning>Update</a>
<a href="admin/delete/<%= user._id %>" class="" btn btn-sm btn-danger>Delete</a>
</td>
</tr>
<% } %>
<% } %>
</tbody>
</table>
</div>
</div>
</div>
我的控制器:
const User = require('../models/User.model');
const getAllUsers = async (req, res) => {
const list = await User.find().exec();
res.render('admin/users', {
users: list
})
}
module.exports = { getAllUsers }
我的路线:
const express = require('express');
const router = express.Router();
const User = require('../models/User.model')
const { authRole, ensureAuthenticated, } = require('../config/auth');
const { getUser, getAllUsers, updateUser, deleteUser } = require('../controllers/user.Controller')
const { json } = require('express/lib/response');
router.get('/', ensureAuthenticated, authRole, (req, res) => {
res.render('admin/admin')
})
router.get('/users', ensureAuthenticated, authRole, getAllUsers);
module.exports = router;
另外,查看我正在使用的数据可能会有所帮助:
[
{
role: 'Admin',
_id: 6217f3160f28e8d7aca39742,
name: 'Matt',
platform: 'Haze0_o',
username: '[email protected]',
email: '[email protected]',
date: 2022-02-24T21:05:26.885Z,
__v: 0
},
{
role: 'User',
_id: 6217f3482d1c8dbce051bb43,
name: 'test3',
platform: 'test3',
username: 'test3',
email: '[email protected]',
date: 2022-02-24T21:06:16.430Z,
__v: 0
},
{
role: 'User',
_id: 62181fd5f85ed0b68c82f33b,
name: 'Jackie',
platform: 'a platform',
username: 'a username',
email: '[email protected]',
date: 2022-02-25T00:16:21.618Z,
__v: 0
}
]
I am currently developing a nodejs app with CRUD operations for the administrative end. This is my table for admins to be able to edit all users. For some reason I am getting an error for Unexpected token '{' in my ejs but I don't see any unclosed brackets or anything of the like. I am confounded. Please let me know if you see the issue here. I'm starting to think the error doesn't have to do with my view and more so with my code. Although I still don't see any issue in my code, a fresh set of eyes will hopefully solve my problem.
My ejs file:
<div class="card">
<div class="card-header">
<div class="row">
<div class="col-8">
<h4 class="font-weight-bold">Users</h4>
</div>
</div>
</div>
<div class="card-body">
<table class="table table-hover">
<thead class="table-dark">
<tr>
<th scope="col">Name</th>
<th scope="col">Role</th>
<th scope="col">Content Creator</th>
<th scope="col">Email</th>
<th scope="col">Username</th>
</tr>
</thead>
<tbody>
<% if(users.length> 0 { %>
<% for (let user of users) { %>
<tr>
<th scope="row">
<%= user.name %>
</th>
<td>
<%= user.role %>
</td>
<td>
<%= user.platform %>
</td>
<td>
<%= user.email %>
</td>
<td>
<%= user.username %>
</td>
<td>
<a href="admin/update/<%= user._id %>" class="" btn btn-sm btn-warning>Update</a>
<a href="admin/delete/<%= user._id %>" class="" btn btn-sm btn-danger>Delete</a>
</td>
</tr>
<% } %>
<% } %>
</tbody>
</table>
</div>
</div>
</div>
My controller:
const User = require('../models/User.model');
const getAllUsers = async (req, res) => {
const list = await User.find().exec();
res.render('admin/users', {
users: list
})
}
module.exports = { getAllUsers }
my route:
const express = require('express');
const router = express.Router();
const User = require('../models/User.model')
const { authRole, ensureAuthenticated, } = require('../config/auth');
const { getUser, getAllUsers, updateUser, deleteUser } = require('../controllers/user.Controller')
const { json } = require('express/lib/response');
router.get('/', ensureAuthenticated, authRole, (req, res) => {
res.render('admin/admin')
})
router.get('/users', ensureAuthenticated, authRole, getAllUsers);
module.exports = router;
Also it may help to see the data I'm working with here:
[
{
role: 'Admin',
_id: 6217f3160f28e8d7aca39742,
name: 'Matt',
platform: 'Haze0_o',
username: '[email protected]',
email: '[email protected]',
date: 2022-02-24T21:05:26.885Z,
__v: 0
},
{
role: 'User',
_id: 6217f3482d1c8dbce051bb43,
name: 'test3',
platform: 'test3',
username: 'test3',
email: '[email protected]',
date: 2022-02-24T21:06:16.430Z,
__v: 0
},
{
role: 'User',
_id: 62181fd5f85ed0b68c82f33b,
name: 'Jackie',
platform: 'a platform',
username: 'a username',
email: '[email protected]',
date: 2022-02-25T00:16:21.618Z,
__v: 0
}
]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来您有一个拼写错误。您缺少 if 语句中的右括号。
这一行:
应该是:
It appears you have a typo. You're missing the closing bracket on your if statement.
This line:
Should be: