Firebase等于无法正常工作
问题描述:
我在firebase中的数据看起来如下所示。Firebase等于无法正常工作
items {
item1: {
field1: value1
field2: value2
}
item2: {
field1: value4
field2: value3
}
}
现在,我想要获取field2 === value3的所有项目。当我运行以下查询时,我得到所有项目。为什么?
firebase.database().ref().child('items')
.orderByChild('field2')
.equalTo('value3')
.limitToLast(10)
.once('value');
答
而不是使用的查询,你可以使用child_added
监听器将返回项目所有子项,然后你可以执行你想要的操作。
firebase.database().ref('items').on('child_added', snap => {
if(snap.val().field2 == 'value3') {
console.log('Match found in ' + snap.key); //Match found in item2
}
});
您是否使用Firebase版本3 API或旧版本2 API?代码看起来不正确。裁判在哪里?使用版本3的API,它会像'firebase.database()。ref()。child(...' – cartant
我使用的是最新的。在代码中,firebase是我的局部变量, .database()。ref()。让我改变问题的代码以获得更好的清晰度 – user43286
你说你会得到'所有物品'。你能得到多少?你能在你的问题中包含收到的物品吗? – cartant