NodeJS 未定义变量导致我的ejs页面无法显示

vyswwuz2  于 12个月前  发布在  Node.js
关注(0)|答案(2)|浏览(148)

if语句中的一个未定义变量导致我的ejs页面无法显示。当我删除if语句时,页面会被渲染和显示,但由于未定义变量和if语句,会发生错误。
我的捐赠者POST路线

app.post('/donors', async (req, res) => {
  try {
    const { bloodType, city } = req.body;
    console.log('Blood Type:', bloodType);
    console.log('City:', city);


    console.log('SQL Query:', query);
    console.log('Parameters:', params);

    const donorsResult = await db.query(query, params);
    const donors = donorsResult.rows;

    // Render the donors.ejs view
    res.render('donors.ejs', { blood_type: bloodType, city_name: city, donors: donors, getBloodTypeText: getBloodTypeText });
  } catch (error) {
    console.error('Error fetching data:', error);
    res.status(500).send('Internal Server Error');
  }
});

字符串
我的捐赠者获取路线

app.get('/donors', async (req, res) => {
  try {
    // Query the database to get distinct blood types
    const bloodTypesResult = await db.query('SELECT DISTINCT blood_type FROM donors');
    const bloodTypes = bloodTypesResult.rows;

    // Query the database to get all cities
   
    const citiesResult = await db.query('SELECT * FROM city');
    // const citiesResult = await db.query('SELECT city_name from donors')
    const cities = citiesResult.rows;

    // Query the database to get a default set of 12 donors
    // Make sure the limit is 12 or a closer number to that amount to display donors informaion by default 
    // prior to the users request
    const defaultDonorsResult = await db.query('SELECT * FROM donors LIMIT 12');
    const defaultDonors = defaultDonorsResult.rows;

    // Render the donors.ejs view and pass the blood types, cities, and default donors as variables
    res.render('donors.ejs', { blood_type: bloodTypes, city: cities, donors: defaultDonors, getBloodTypeText: getBloodTypeText, getCompatibleBloodTypes: getCompatibleBloodTypes});
  } catch (error) {
    console.error('Error fetching data:', error);
    // Handle other errors if needed
    res.status(500).send('Internal Server Error');
  }
});


第二个捐助者GET路径呈现页面

app.get('/donors', (req, res) => {
  res.render('donors.ejs', {});
});


第三捐助者获得途径

app.get('/views/donors.ejs', (req, res) => {
  res.render('donors.ejs', {});
});


donors.ejs代码

<div class="container mt-5">
  <div class="row justify-content-center">
    <% if (donors && donors.length > 0) { %>
      <% donors.forEach(donor => { %>
        <div class="col-12 col-sm-6 col-lg-4 mb-4">
          <div class="card" style="background-color: #D27878;">
            <div class="card-body">
              <h5 class="card-title"><%= donor.full_name %></h5>
              <p class="card-text">Blood Type: <%= donor.blood_type %></p>
              <p class="card-text">City: <%= donor.city_name %></p>
              <p class="card-text">Phone: <%= donor.phone_number %></p>
            </div>
          </div>
        </div>
      <% }); %>
    <% } else { %>
      <p>No donors found.</p>
    <% } %>
  </div>
</div>


在if语句之前未声明donors变量
仅当在URL中键入路由时才显示该页面,但是如果单击按钮将用户定向到donator.ejs页面,则会出现以下错误

ReferenceError: C:\Users\kinga\Downloads\BackendGraduationProject\HopeDrops website\views\donors.ejs:98
    96| <div class="container mt-5">

    97|   <div class="row justify-content-center">

 >> 98|     <% if (donors && donors.length > 0) { %>

    99|       <% donors.forEach(donor => { %>

    100|         <div class="col-12 col-sm-6 col-lg-4 mb-4">

    101|           <div class="card" style="background-color: #D27878;">

donors is not defined

cyvaqqii

cyvaqqii1#

是的,因为你的第二个和第三个代码段没有传递donors变量,所以错误是完全正确的。如果donors是必需的,那么你最好每次都提供它>

app.get('/donors', (req, res) => {
  res.render('donors.ejs', {donors: 0});
});

app.get('/views/donors.ejs', (req, res) => {
  res.render('donors.ejs', {donors: 0});
});

字符串

gg0vcinb

gg0vcinb2#

res.render('donors.ejs', {})中,没有变量传递给视图({}是空的)。所以在视图中没有定义donors变量,你永远不会传递它。
也许你应该传递undefined

res.render('donors.ejs', { donors: undefined })

字符串
你可能有同样的问题,你没有传递其他值。

相关问题