jon*_*ne2 0 javascript firebase firebase-realtime-database
我想查询包含输入的 Firebase。例如在下面的数据库结构中,我想查询包含“4140”的所有节点,所以我使用此代码
var catref = Cataloguedatabase.ref("/Listing Search/");
return catref.once('value').then(function(snapshot){
snapshot.forEach(childSnapshot => {
let snapdb = childSnapshot.val();
let key = childSnapshot.key;
//use ES6 includes function
if(key.includes(schname)){
console.log(childSnapshot.val());
var searchresults = childSnapshot.val();
var container = document.getElementById('listing_gallery');
// container.innerHTML = '';
var productCard = `
<div class="col-sm-3">
<div class="card" onclick="gotoproduct(this);" id="${key}">
`
container.innerHTML += productCard;
}
})
})
Run Code Online (Sandbox Code Playgroud)
这行得通,但问题是它首先查询所有子节点并对数组进行排序。这对于子节点很少的节点来说是可以的,但是当它有数千个子节点时,它变得不切实际。有没有我只能查询包含 firebase 中的值的键,如果没有,我还能如何实现?
正如 Doug 所说,无法查询包含特定字符串的值。但是您可以查询以某个子字符串开头的键:
return catref.orderByKey().startAt("4140-").endAt("4140-\uf8ff").once('value').then(function(snapshot){
Run Code Online (Sandbox Code Playgroud)
这将只匹配键以 开头的子节点4140-。