gpt4 book ai didi

javascript - firebase 查询方法 startAt() 采用区分大小写的参数

转载 作者:可可西里 更新时间:2023-11-01 01:18:26 26 4
gpt4 key购买 nike

这段代码工作正常。

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

最佳答案

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

关于javascript - firebase 查询方法 startAt() 采用区分大小写的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38590937/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com