firebase查询方法startAt()采用区分大小写的参数

Ank*_*ari 17 javascript firebase firebase-realtime-database

这段代码工作正常.

我想要的唯一改进是 - 当我传递"Pi"时,它会获取以名称"Pi"开头的所有项目对象,但是当我输入"pi"时它什么都不返回!

这意味着我希望这种方法startAt(itemName)不区分大小写.所以这应该适用于任何事情(小写或大写),在这种情况下"Pi"或"pi"等.

//5. Get menu items from RestaurantMenu
this.getMenuItemFromRestaurantMenu = function(callback, itemName) {
  var ref_restMenu = firebase.database().ref()
  .child('Restaurants')
  .child('Company')
  .child('menu');

  //Check if item is already exist!
  ref_restMenu.orderByChild("itemName").startAt(itemName).once("value", function(snapshot) {
    var data = snapshot.val(); 
    if(data !== null) {
      //We will ger item name and restaurant id from this data.
      callback(data);
    } else {
      //Item not found in globalMenu
      console.log("%c Item not found in Global Menu", "color: red");
    }
  });
}
Run Code Online (Sandbox Code Playgroud)

the*_*het 15

Firebase目前不支持小写搜索.处理此问题的最佳方法是将小写字符串与原始字符串一起存储,然后查询小写字符串.

var ref_restMenu = firebase.database().ref()
    .child('Restaurants')
    .child('Company')
    .child('menu');
var item = "Apple Pie";
// Or however you store data
ref.push({
    itemName: item,
    itemNameLower: item.toLowerCase(),
    ...
})
Run Code Online (Sandbox Code Playgroud)

然后你可以这样查询:

//Check if item is already exist!
// query itemNameLoweruse and .toLowerCase()
ref_restMenu.orderByChild("itemNameLower").startAt(itemName.toLowerCase()).once("value", function(snapshot) {
    var data = snapshot.val(); 
    if(data !== null) {
        //We will ger item name and restaurant id from this data.
        callback(data);
    } else {
        //Item not found in globalMenu
        console.log("%c Item not found in Global Menu", "color: red");
    }
});
Run Code Online (Sandbox Code Playgroud)

这确实需要复制数据,但截至目前,没有一个更容易预见的选择.

参考:Firebase Google论坛

  • 然后显示非小写版本.您只能按小写查询,这并不意味着您需要提供它. (2认同)