2016-07-26 88 views
8

此代碼工作正常!firebase查詢方法startAt()考慮區分大小寫參數

我想唯一的改進是 - 當我通過「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"); 
 
    } 
 
    }); 
 
}

回答

10

目前在火力地堡小寫搜索的支持。處理此問題的最佳方法是將小寫字符串存儲在原始字符串的一側,然後查詢小寫字符串。

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

+0

謝謝,這幫了我! –

+0

這個問題是當你想顯示用戶註冊的方式時。 – EduardoMaia

+2

然後顯示非小寫版本。你只能用小寫查詢,這並不意味着你需要提交它。 – theblindprophet