【问题标题】:Javascript filtering data from FirestoreJavascript 过滤来自 Firestore 的数据
【发布时间】:2020-07-07 00:40:18
【问题描述】:

我正在为 Firestore 中的数据创建一个简单的过滤器。我的数据库中有 4 个对象,问题是每个过滤器从集合中输出相同的最后一个对象。我相信问题是当我从<select> 标记分配<option> 的值时......我想存储我从字段中选择的内容,然后使用该属性从数据库中过滤。

我为此提供了 HTML 和 JavaScript 代码。

let applyButton = document.getElementById("apply-id");

let categoryType = document.getElementById("select-by-type");
let categoryTypeOption = categoryType.getElementsByTagName("option")[categoryType.selectedIndex]
                     .getAttribute("value");
                     
let categoryServer = document.getElementById("select-by-server");
let categoryServerOption = categoryServer.getElementsByTagName("option")[categoryServer.selectedIndex]
                     .getAttribute("value");
                     
let categoryPrice = document.getElementById("select-by-price");
let categoryPriceOption = categoryPrice.getElementsByTagName("option")[categoryPrice.selectedIndex]
                     .getAttribute("value");


    applyButton.addEventListener("click", function() {
      if (categoryPriceOption == "high") {
        categoryPriceOption = "'price', 'desc'";
      } else {
        categoryPriceOption = "price"; 
      }
      firebase.firestore().collection("categories")
        .where("categoryType", "==", `${categoryTypeOption}`)
        .where("categoryServer", "==", `${categoryServerOption}`)
        .orderBy(categoryPriceOption)
        .get()
        .then(function(querySnapshot) {
          querySnapshot.forEach(function(doc) {
              console.log(doc.id, " => ", doc.data());
          });
      })
    });
<div id="filter-modal-id" class="modal-general">
          <div class="main-intro-to-verification" style="border: 0;;">
            Filter
          </div>

          <div class="verification-main-input-div" style="margin-top: 0;">
            <p class="verification-main-text">By type</p>

            <div class="drop-dawn-add-category-list">
              <select id="select-by-type" name="cars" class="drop-dawn-add-ctegory-select">
                <option value="Clothing">Clothing</option>
                <option value="Shoes">Shoes</option>
                <option value="Face">Face</option>
                <option value="Body">Body</option>
              </select>
            </div>
          </div>

          <div class="verification-main-input-div">
            <p class="verification-main-text">By location</p>

            <div class="drop-dawn-add-game-list">
              <select id="select-by-server" name="cars" class="drop-dawn-add-category-select">
                <option value="EU">EU</option>
                <option value="USA">USA</option>
              </select>
            </div>
          </div>

          <div class="verification-main-input-div">
            <p class="verification-main-text">By price</p>

            <div class="drop-dawn-add-category-list">
              <select id="select-by-price" name="cars" class="drop-dawn-add-category-select">
                <option value="high">High</option>
                <option value="low">Low</option>
              </select>
            </div>
          </div>


          <div class="main-order-action-buttons" style="margin-top: 25px ; border-bottom: 0;">
            <div id="apply-id" class="all-order-action-buttons">Apply</div>
            <div id="cancel-id" class="all-order-action-buttons">Cancel</div>
        </div>
    </div>

【问题讨论】:

    标签: javascript firebase google-cloud-firestore


    【解决方案1】:

    您不能将要排序的字段及其 desc 修饰符在单个字符串中传递给 API。您需要将它们作为单独的参数传递。

    类似:

      let query = firebase.firestore().collection("categories")
        .where("categoryType", "==", categoryTypeOption)
        .where("categoryServer", "==", categoryServerOption);
      if (categoryPriceOption == "high") {
        query = query.orderBy('price', 'desc');
      } else {
        query = query.orderBy('price');
      }
      query
        .get()
        .then(function(querySnapshot) {
          querySnapshot.forEach(function(doc) {
              console.log(doc.id, " => ", doc.data());
          });
      })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-26
      • 1970-01-01
      • 2021-11-24
      • 1970-01-01
      • 2020-07-23
      • 2020-07-09
      • 1970-01-01
      • 2021-09-18
      相关资源
      最近更新 更多