使用 AngularJS 在 Firebase 实时数据库中使用 .indexOn 规则过滤数据
Filter data with .indexOn rules in Firebase realtime database with AngularJS
我正在使用如下所示的 FireBase 实时数据库结构。并想要提取所有具有 red
颜色的 model。
FireBase
{
"models": {
"model1": {
"modelName": "model 1",
"color": {
"red": true,
"blue": true,
"green": true
}
},
"model2": {
"modelName": "model 2",
"color": {
"yellow": true,
"blue": true,
"green": true
}
},
"model3": {
"modelName": "model 3",
"color": {
"red": true,
"yellow": true,
"pink": true
}
},
"model4": {
"modelName": "model 4",
"color": {
"red": true,
"orange": true,
"white": true
}
}
}
}
我尝试使用下面的查询但没有得到预期的结果。
AngularJS
let fbRef = firebase.database().ref('models');
fbRef
.orderByChild('red')
.equalTo(true)
.once('value', function(data){
console.log('result : ', data);
});
FireBase 规则
{
"rules": {
".read": true,
"$modelID": {
".read": true,
".indexOn": ["red", "blue", "green", "orange", "pink", "white", "yellow"]
}
}
}
预期结果
{
"model1": {
"modelName": "model 1",
"color": {
"red": true,
"blue": true,
"green": true
}
},
"model3": {
"modelName": "model 3",
"color": {
"red": true,
"yellow": true,
"pink": true
}
},
"model4": {
"modelName": "model 4",
"color": {
"red": true,
"orange": true,
"white": true
}
}
}
您应该在'color/red'
之前订购,如下:
fbRef
.orderByChild('color/red')
.equalTo(true)
.once('value', function(data) {
//Use the val() method of the DataSnapshot
console.log('result : ', data.val());
//Or loop over children
data.forEach(function(childSnapshot) {
var childKey = childSnapshot.key;
var childData = childSnapshot.val();
console.log('childKey : ', childKey);
console.log('childData : ', childData);
});
});
因为你得到了 DataSnapshot
, you may either use the val()
method to get the result as an object or use forEach()
到 "enumerates the top-level children in the DataSnapshot"。两种方式的区别之一是 "the ordering of data in the JavaScript object returned by val()
is not guaranteed to match the ordering on the server" 如文档中所述。
我正在使用如下所示的 FireBase 实时数据库结构。并想要提取所有具有 red
颜色的 model。
FireBase
{
"models": {
"model1": {
"modelName": "model 1",
"color": {
"red": true,
"blue": true,
"green": true
}
},
"model2": {
"modelName": "model 2",
"color": {
"yellow": true,
"blue": true,
"green": true
}
},
"model3": {
"modelName": "model 3",
"color": {
"red": true,
"yellow": true,
"pink": true
}
},
"model4": {
"modelName": "model 4",
"color": {
"red": true,
"orange": true,
"white": true
}
}
}
}
我尝试使用下面的查询但没有得到预期的结果。
AngularJS
let fbRef = firebase.database().ref('models');
fbRef
.orderByChild('red')
.equalTo(true)
.once('value', function(data){
console.log('result : ', data);
});
FireBase 规则
{
"rules": {
".read": true,
"$modelID": {
".read": true,
".indexOn": ["red", "blue", "green", "orange", "pink", "white", "yellow"]
}
}
}
预期结果
{
"model1": {
"modelName": "model 1",
"color": {
"red": true,
"blue": true,
"green": true
}
},
"model3": {
"modelName": "model 3",
"color": {
"red": true,
"yellow": true,
"pink": true
}
},
"model4": {
"modelName": "model 4",
"color": {
"red": true,
"orange": true,
"white": true
}
}
}
您应该在'color/red'
之前订购,如下:
fbRef
.orderByChild('color/red')
.equalTo(true)
.once('value', function(data) {
//Use the val() method of the DataSnapshot
console.log('result : ', data.val());
//Or loop over children
data.forEach(function(childSnapshot) {
var childKey = childSnapshot.key;
var childData = childSnapshot.val();
console.log('childKey : ', childKey);
console.log('childData : ', childData);
});
});
因为你得到了 DataSnapshot
, you may either use the val()
method to get the result as an object or use forEach()
到 "enumerates the top-level children in the DataSnapshot"。两种方式的区别之一是 "the ordering of data in the JavaScript object returned by val()
is not guaranteed to match the ordering on the server" 如文档中所述。