我可以确定查询是否找不到具有指定范围内的值的任何子项? [英] Can I determine if query does NOT find any children with values within a specified range?

查看:106
本文介绍了我可以确定查询是否找不到具有指定范围内的值的任何子项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以通过某种方法确定 ref 中是否存在 -1 newTotal ?



这是我希望能够工作的。如果子查询中存在子查询,则回调运行。

  ref.orderByValue()。startAt(-1).endAt(newTotal) .limitToLast(1).once(child_added,function(snap){

/ *只在查询产生结果时运行* /

if(snap.exists( )){
...
} else {
...
}

});

newTotal 是0或任何正整数。 为了检测什么时候没有孩子,你需要(也)监听的值。事件:

$ p $ 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');
}
});


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.

解决方案

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');
  }    
});

这篇关于我可以确定查询是否找不到具有指定范围内的值的任何子项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆