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

IT技术 javascript firebase firebase-realtime-database
2021-01-28 11:23:10

这段代码工作正常。

我想要的唯一改进是- 当我传递“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");
    }
  });
}
1个回答

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(),
    ...
})

然后你可以这样查询:

//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");
    }
});

这确实需要复制数据,但到目前为止,还没有更容易预见的选择。

参考:Firebase Google 论坛

然后显示非小写版本。您只能按小写查询,这并不意味着您需要呈现它。
2021-03-18 11:23:10
问题是在查询时您想显示用户注册的方式。
2021-04-05 11:23:10