【发布时间】:2022-04-03 03:52:32
【问题描述】:
我正在尝试使用 ejs 将 mongodb 文档中的信息显示到我的网页上,但我不断收到“信标”不是防御错误,我不知道这是怎么回事,我到处找。
这是我得到的错误:
ReferenceError: C:\Users\sahil\Downloads\node_passport_login-master\views\dashboard.ejs:5
3| <div class="beacons content">
4| <h2>All beacons</h2>
>> 5| <% if (Beacon.length > 0) { %>
6| <% Beacon.forEach(beacon => { %>
7| <a class="single" href="/Beacon/<%=Beacon._id %>">
8| <h3 class="title"><%= beacon.title %></h3>
Beacon is not defined
at eval ("C:\\Users\\sahil\\Downloads\\node_passport_login-master\\views\\dashboard.ejs":12:8)
at dashboard (C:\Users\sahil\Downloads\node_passport_login-master\node_modules\ejs\lib\ejs.js:692:17)
at tryHandleCache (C:\Users\sahil\Downloads\node_passport_login-master\node_modules\ejs\lib\ejs.js:272:36)
at View.exports.renderFile [as engine] (C:\Users\sahil\Downloads\node_passport_login-master\node_modules\ejs\lib\ejs.js:489:10)
at View.render (C:\Users\sahil\Downloads\node_passport_login-master\node_modules\express\lib\view.js:135:8)
at tryRender (C:\Users\sahil\Downloads\node_passport_login-master\node_modules\express\lib\application.js:640:10)
at Function.render (C:\Users\sahil\Downloads\node_passport_login-master\node_modules\express\lib\application.js:592:3)
at ServerResponse.render (C:\Users\sahil\Downloads\node_passport_login-master\node_modules\express\lib\response.js:1017:7)
at ServerResponse.res.render (C:\Users\sahil\Downloads\node_passport_login-master\node_modules\express-ejs-layouts\lib\express-layouts.js:77:18)
at C:\Users\sahil\Downloads\node_passport_login-master\routes\index.js:10:7
下面的代码是我的控制器
const beacon_dashboard = (req, res) => {
Beacon.find().sort({ createdAt: -1 })
.then(result => {
res.render('dashboard', { beacons: result, name: 'beacons' });
})
.catch(err => {
console.log(err);
});
}
我的路由器
const express = require('express');
const beaconController = require('../controllers/beaconController.js');
const router = express.Router();
router.get('/', beaconController.beacon_dashboard);
router.get('/create', beaconController.beacon_create_get);
router.post('/', beaconController.beacon_create_post);
router.get('/:id', beaconController.beacon_details);
router.delete('/:id', beaconController.beacon_delete);
module.exports = router;
我的 ejs
<div class="row mt-5">
<div class="col-md-6 m-auto">
<div class="beacons content">
<h2>All beacons</h2>
<% if (beacons.length > 0) { %>
<% beacons.forEach(beacon => { %>
<a class="single" href="/beacons/<%=beacon._id %>">
<h3 class="title"><%= beacon.title %></h3>
<p class="snippet"><%= beacon.snippet %></p>
</a>
<% }) %>
<% } else { %>
<p>There are no blogs to display...</p>
<% } %>
</div>
</div>
</div>
【问题讨论】:
标签: javascript node.js mongodb express ejs