Can I determine if query does NOT find any children with values within a specified range?

霸气de小男生 提交于 2019-12-31 03:59:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!