【问题标题】:firebase query methods startAt() taking case sensitive parametersfirebase 查询方法 startAt() 采用区分大小写的参数
【发布时间】:2016-11-30 03:56:27
【问题描述】:

这段代码运行良好。

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

【问题讨论】:

    标签: javascript firebase firebase-realtime-database


    【解决方案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 Forum

    【讨论】:

    • 这个问题是在查询时要显示用户注册的方式。
    • 然后显示非小写版本。只用小写查询,不代表需要呈现。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-26
    • 2013-04-25
    • 2023-03-15
    • 1970-01-01
    相关资源
    最近更新 更多