Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

firebase query methods startAt() taking case sensitive parameters

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");
    }
  });
}
like image 314
Ankit Maheshwari Avatar asked Jul 26 '16 13:07

Ankit Maheshwari


People also ask

Are firebase queries case sensitive?

David Notik. Ok, so Firebase is case-sensitive so if you're users/Kai is not same as users/kai.

What is addValueEventListener in firebase?

public interface ValueEventListener. Classes implementing this interface can be used to receive events about data changes at a location. Attach the listener to a location user addValueEventListener(ValueEventListener) .

Which query language is used in firebase?

Firebase uses NoSQL; MySQL uses SQL.

What are the real time database limits in firebase?

256 MB from the REST API; 16 MB from the SDKs. The total data in each write operation should be less than 256 MB. Multi-path updates are subject to the same size limitation. The total bytes written through simultaneous write operations on the database at any given time.


1 Answers

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

like image 118
theblindprophet Avatar answered Oct 02 '22 07:10

theblindprophet