如果您的代码看起来是“无限迭代”,那是因为您在第一个查询中使用了on()方法。事实上
on()
方法“在特定位置侦听数据更改”,如前所述
here
.
如果只想查询一次引用,请使用
once()
here
.
以下是一个
Query
orderByChild()
方法
Reference
equalTo()
方法)。
ref.orderByChild("index").equalTo(currentIndex)
如前所述
here
即使查询只有一个匹配项,快照也是
需要循环检查结果:
ref.once('value', function(snapshot) {
snapshot.forEach(function(childSnapshot) {
var childKey = childSnapshot.key;
var childData = childSnapshot.val();
// ...
});
});
ref.orderByChild("index").equalTo(currentIndex).once("value", function(snapshot) {
snapshot.forEach(function(childSnapshot) {
console.log(childSnapshot.val().first);
console.log(childSnapshot.val().date);
});
});