代码之家  ›  专栏  ›  技术社区  ›  Kevin Dias

如果数据不存在,FireBase查询不显示任何数据

  •  0
  • Kevin Dias  · 技术社区  · 7 年前

    FireBase查询不显示任何数据如果数据不存在,则不显示任何空值、未定义、无任何消息,它仅显示在数据库中找到某些内容时的情况。

    const-ref=this.fb.database().ref(“用户”);
    var self=这个;
    
    ref.orderbychild('cellphone').equalto('+351'+this.phoneNumber.toString()).on(“已添加子项”,(快照)=>。{
    console.log(snapshot.val().cellphone);
    
    if(snapshot.exists()){
    让toast=self.toastctrl.create({
    message:'手机已经存在',
    持续时间:2000,
    位置:“底部”
    (});
    
    toast.present();
    }
    
    如果(!)snapshot.exists())//它甚至不能到达这里
    console.log('can register');
    }
    
    (});
    < /代码> 
    
    

    我的数据库结构,所以如果它找不到我的手机,就一点也不开枪。

    .

      const ref =  this.fb.database().ref("Users");
         var self = this;
    
          ref.orderByChild('cellphone').equalTo('+351' + this.phoneNumber.toString()).on("child_added", (snapshot) => {
          console.log(snapshot.val().cellphone);
    
            if (snapshot.exists()){
              let toast = self.toastCtrl.create({
                message: 'Cellphone already exist',
                duration: 2000,
                position: 'bottom'
              });
    
              toast.present();
            }
    
            if(!snapshot.exists()){ // it dont even get here 
              console.log('can register');
            }
    
        });
    

    我的数据库结构,所以如果它找不到我的手机,就一点也不开枪。

    1 回复  |  直到 7 年前
        1
  •  0
  •   Kevin Dias    7 年前

    解决方案是 using "value" instead of "child_added"

    这个 child_added 为查询的节点下的每个匹配子级激发事件。如果没有匹配的子项,则不会激发。

    这意味着要测试节点是否存在,必须使用 value 处理程序。由于一个查询可以有多个结果,所以需要循环访问子查询。

    推荐文章