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

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

问题描述

Pi,它将获取所有以Pi开始的项目对象,但是当我输入pi时,它不会返回任何东西!

这意味着我想要这个方法 .startAt(itemName)在工作时不区分大小写。所以这应该与任何事情(小写或大写),在这种情况下Pi或丕等。

  // 5。从RestaurantMenuthis.getMenuItemFromRestaurantMenu = function(callback,itemName){var ref_restMenu = firebase.database().ref().child('Restaurants').child('Company').child('menu'); //检查项目是否已经存在! (itemName)。startAt(itemName).once(value,function(snapshot){var data = snapshot.val(); if(data!== null){//我们将会把项目名称和数据库中的餐厅ID callback(data);} else {//在globalMenu console.log中找不到项目(%c在全局菜单中找不到项目,color:red);}});}  


解决方案

支持Firebase中的小写搜索。处理这种情况的最佳方法是将小写字符串存储在原始字符串的旁边,然后查询小写字符串。

  var ref_restMenu = firebase.database()。ref()
.child('Restaurants')
.child('Company')
.child('menu');
var item =Apple Pie;
//或者你存储数据
ref.push({
itemName:item,
itemNameLower:item.toLowerCase(),
...





$ b然后你可以这样查询:

  //检查项目是否已经存在! 
//查询itemNameLoweruse和.toLowerCase()
ref_restMenu.orderByChild(itemNameLower)。startAt(itemName.toLowerCase())。once(value,function(snapshot){
var data = snapshot.val();
if(data!== null){
//我们将从这个数据中删除项目名称和餐厅ID
callback(data);
} else {
//在globalMenu中找不到的项目
console.log(%c在全局菜单中找不到的项目,color:red);
}
});

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

参考: Firebase Google论坛


This code is working fine!!

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!

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

解决方案

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

Then you could query as so:

//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.

Reference: Firebase Google Forum

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

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