firebase 查询方法 startAt() 采用区分大小写的参数 [英] firebase query methods startAt() taking case sensitive parameters

查看:26
本文介绍了firebase 查询方法 startAt() 采用区分大小写的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码运行良好.

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

The only improvement I want is - when I pass "Pi", it fetch all the items object that begin with the name "Pi", but when I enter "pi" it returns nothing!

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

This means I want this method startAt(itemName) to be worked case insensitive. So that should works with anything (lowercase or uppercase) in that case "Pi" or "pi" etc..

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

推荐答案

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

There is currently no support for lowercase searches in Firebase. The best way to handle this would be store the lowercase string along side the original string and then query the lowercase string instead.

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

这确实需要复制数据,但目前还没有更容易预见的选项.

This does require replicating the data, but as of now there is not an easier foreseeable option.

参考:Firebase Google 论坛

这篇关于firebase 查询方法 startAt() 采用区分大小写的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆