【问题标题】:Search Firebase Database using key with search bar使用带有搜索栏的键搜索 Firebase 数据库
【发布时间】:2019-02-20 15:36:24
【问题描述】:

我在 firebase 数据库中有数据,数据的结构如附图所示。当用户在应用程序中输入搜索查询时,我正在尝试使用搜索栏搜索食谱的名称。当前设置的索引规则也显示在附图中。这是我的代码:

self.findRecipes(text: "Lemon")

func findRecipes(text: String)->Void{

    ref.child("Categories").queryOrdered(byChild: "recipeName").queryStarting(atValue: text).queryEnding(atValue: text+"\u{f8ff}").observe(.value, with: { snapshot in

        print(snapshot)

    })
}

控制台中的输出是

Snap (Categories) <null> 

有什么帮助吗?

【问题讨论】:

  • 你错过了一个关卡。 recipeName 节点不是 /Categories/ 子节点的子节点。它是 /Categories/Beverages/ 中子节点的子节点,因此您需要将 /Beverages/ (或 /Breakfast & Brunch)添加到 ref 路径以进行查询。如果您需要执行较浅的查询,请考虑将所有节点保留在父类别节点中,并添加一个子节点来定义它是什么类型 /Categories/childByAutoId/recipeName: "xxxx" 和另一个子节点/recipeType: "饮料"

标签: ios swift firebase search indexing


【解决方案1】:

抱歉,我只知道如何使用 webapps 中的 firestore DB 中的搜索栏成功检索数据。也许它可以帮助你。

HTML:

<input type="text" placeholder="search" id="latestHotDogStatus"> 
<ul id="pastComments2"></ul>
<li><id="loadButton">Search</li> 

JS:

firebase.initializeApp(config);
var firestore = firebase.firestore();
var docRef = firestore.collection("indian");
var showat2 = document.querySelector("#pastComments2");
var loadbutton = document.querySelector("#loadButton");
var inputTextField = document.querySelector("#latestHotDogStatus");
loadbutton.addEventListener("click",function(){
    docRef.get()
    .then(snapshot => {
      snapshot.forEach(doc => {
        const myData = doc.data();
                        if(myData.dname == inputTextField.value){

               showat2.innerHTML += myData.dname+" "+myData.dno+myData.no+ ;  
                  inputTextField.innerHTML = " ";
        }                  

      });
    }).catch(function(error){
    console.log("error: "+error);
  });
});

【讨论】: