问题
Can I somehow determine if no children at ref exist with vals between -1 and newTotal?
Here's what I was hoping would work. The callback runs if children exist within the query.
ref.orderByValue().startAt(-1).endAt(newTotal).limitToLast(1).once("child_added", function(snap){
/* Runs only when query yields a result */
if (snap.exists()){
...
} else {
...
}
});
newTotal is 0 or any positive integer.
回答1:
The child_ events will only fire when the relevant child exists/changes.
To detect when there are no children, you'll need to (also) listen for the value event:
var query = ref.orderByValue().startAt(-1).endAt(newTotal).limitToLast(1)
query.once("value", function(snapshot){
if (snapshot.exists()){
snapshot.forEach(function(child) {
..
});
} else {
console.log('No child exists');
}
});
来源:https://stackoverflow.com/questions/39900082/can-i-determine-if-query-does-not-find-any-children-with-values-within-a-specifi