【问题标题】:Ejs error keeps telling me my 'beacon' is not definedEjs 错误一直告诉我我的“信标”未定义
【发布时间】: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


    【解决方案1】:

    尝试使用:

    <% for(var i=0;i<beacon.length;i++) { %>
        <a class="single" href="/beacons/<%=beacon[i]._id %>">
            <h3 class="title"><%= beacon[i].title %></h3>
            <p class="snippet"><%= beacon[i].snippet %></p>
        </a>
    <% } %>
    

    【讨论】:

      【解决方案2】:

      根据 ReferenceError,您的 Beacon(单数和 B 大写)但在您的代码中,它的信标与您的 ejs 和控制器文件中的信标相同,应该可以正常工作,我相信这是区分大小写的情况在你的代码中看看它应该可以工作

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-11
        • 2017-01-16
        • 1970-01-01
        相关资源
        最近更新 更多