检查Firebase中是否存在值 [英] Check if a value exits in Firebase

查看:81
本文介绍了检查Firebase中是否存在值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我客户的节点

customers
 {
 "-KXKCee0yVJE1z1gCQcV" : {
"cust_id" : "-KXKCee0yVJE1z1gCQcV",
"email" : "xxx@xxx.com",
"food_pref" : "nonveg",
"name" : "Jind",
"notes" : "",
"phone" : "123"
 },
 "-KXMVChcSwfUif-xVbRa" : {
"cust_id" : "-KXMVChcSwfUif-xVbRa",
"email" : "ggg@ggg.com",
"food_pref" : "nonveg",
"name" : "Mani",
"notes" : "Butter Chicken!!!",
"phone" : "456"
  },
  "-KXV4eYM6bKNniE_oLEf" : {
"cust_id" : "-KXV4eYM6bKNniE_oLEf",
"email" : "vvv@vvv.com",
"food_pref" : "veg",
"name" : "Jay",
"notes" : "",
"phone" : "789"
 }
}

我正在使用以下查询来检查电话号码

I'm using the following query to check Phone Number

Query query = reference.child("customers").orderByChild("phone").equalTo("789");

如何检查值"789"是否存在?

How do I check if the value "789" exists?

尝试做

 query.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            if (dataSnapshot.exists())
                Log.d(TAG, dataSnapshot.getKey());
            else
                Log.d(TAG, "Not Found");
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {

        }

这不起作用.是否有其他选择可以检查?

This does not work.Any alternate for checking this?

推荐答案

您应使用仅当子项存在时,才会调用ChildEventListener.如果没有,则什么也不会发生.

The ChildEventListener will be invoked only if the child exists. If it doesn't, nothing will happen.

将始终调用ValueEventListener,并且您可以调用快照的

The ValueEventListener will always be invoked and you can call the snapshot's exists() method to check for existence.

这篇关于检查Firebase中是否存在值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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