postgresql 有些数据没有被提取

31moq8wy  于 2024-01-07  发布在  PostgreSQL
关注(0)|答案(1)|浏览(130)

在页面中,有一个表单,其中包含两个列表,血型,城市(城市名称)和一个按钮。当点击按钮时,表单下方会显示血型匹配的献血者和匹配城市的信息。在我的postgres中,有一个位于六个不同城市的不同血型的献血者表的数据量很大。
该页面显示了六个显示捐献者信息的默认卡片,我们可以清楚地看到,例如,一个血型为AB-的捐献者和城市安曼,但是当我输入并提交这两个值时,页面在表单下方显示一条消息:没有找到捐献者。
EJS表格

<!--start search-->
  <div class="search pt-5 ">
    <div class="container">
      <div class="row justify-content-center">
        <div class="searchBox col-11 rounded-3 shadow p-5 position-relative">
          <form action="donors" method="post" class="d-flex flex-column flex-xl-row gap-3 text-center">   
            <div class=" w-100">
           
              <select name="bloodType" class="form-select form-select-lg" required>
                <option selected hidden style="display:none" value="">Blood Type</option>
                <option value="A+">A+</option>
                <option value="A-">A-</option>
                <option value="B+">B+</option>
                <option value="B-">B-</option>
                <option value="O+">O+</option>
                <option value="O-">O-</option>
                <option value="AB+">AB+</option>
                <option value="AB-">AB-</option>
            </select>
            </div>
            <div class=" w-100">
            <select name="city" class="form-select form-select-lg" required>
                <option selected hidden style="display:none" value="">City</option>
                <option value="amman">Amman</option>
                <option value="madaba">Madaba</option>
                <option value="irbid">Irbid</option>
                <option value="zarqa">Zarqa</option>
                <option value="mafraq">Mafraq</option>
                <option value="ajloun">Ajloun</option>
            </select>
            </div>
            <div class="w-100">

            </div>
            <div class=" w-100">

              <!-- <a class="btn searchBtn fw-bold" href="#" type="submit"><i class="fa-solid fa-magnifying-glass fa-lg"></i></a> -->

              <button class="btn searchBtn fw-bold" type="submit">
                <i class="fa-solid fa-magnifying-glass fa-lg"></i>
              </button>
              
            </div>
          </form> 
        </div>         
      </div>
    </div>
  </div>
  


  
<!--end search-->

<!--start Donors-->


<div class="container mt-5">
  <div class="row justify-content-center">
    <% if (donors && donors.length > 0) { %>
      <% donors.forEach(donor => { %>
          <!-- Render donor card using donor information -->
          <div class="col-12 col-sm-6 col-lg-4 mb-4">
              <!-- customize the donor card layout based on your needs -->
              <div class="card" style="background-color: #D27878;" data-blood-type-index="<%= donor.blood_type %>">
                  <div class="card-body">
                      <h5 class="card-title"><%= donor.full_name %></h5>
                      <p class="card-text">Blood Type: <%= getBloodTypeText(donor.blood_type) %></p>
                      <p class="card-text">City: <%= donor.city_name %></p>
                      <p class="card-text">Phone: <%= donor.phone_number %></p>
                      <!-- add more donor information as needed -->
                  </div>  
              </div>
          </div>
      <% }); %>
  <% } else { %>
      <p>No donors found.</p>
  <% } %>
  </div>
</div>

字符串
我的Node js文件中的函数:

const getBloodTypeText = (bloodType) => {
  // Optionally, you can check if the blood type is one of the expected values
  const validBloodTypes = ['A+', 'A-', 'B+', 'B-', 'O+', 'O-', 'AB+', 'AB-'];
  if (validBloodTypes.includes(bloodType)) {
      return bloodType;
  } else {
    return 'Unknown Blood Type';
  }
};


献血者邮政路线

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

      let query = 'SELECT * FROM donors WHERE city_name = $1';
      const params = [city];

      if (bloodType) {
          query += ' AND blood_type = $2';
          params.push(bloodType);
      }

      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, getCompatibleBloodTypes: getCompatibleBloodTypes`` });
  } 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 cities = citiesResult.rows;

    // Query the database to get a default set of 6 donors
    const defaultDonorsResult = await db.query('SELECT * FROM donors LIMIT 6');
    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');
  }
});


下面应该会出现献血者的匹配信息,但是这个页面只适用于A+血型,至于其他血型,页面上写着:没有找到献血者。不是所有血型或城市都有效

von4xj4u

von4xj4u1#

代码中的两个(可能的)错误:

SELECT * FROM donors WHERE city_name = amman;

字符串
不正确,因为city_name应该有单引号,例如:

SELECT * FROM donors WHERE city_name = 'amman';


其次,您的城市名称是“安曼”,而不是“安曼”,因此您应该进行不区分大小写的检查

SELECT * FROM donors WHERE LOWER(city_name) = 'amman';


但这可能会导致性能问题,所以这可能会更快:

SELECT * FROM donors WHERE city_name ILIKE 'amman';


因为ILIKE正在进行不区分大小写的检查。
上面的SQL可以在这里找到:DBFIDDLE
P.S. blood_type也存在同样的问题。

相关问题