简体   繁体   中英

Firebase Query returning null value but the value exists

I am creating a phone node and storing user's number in that node at the time of sign up so that app asks him to sign up and does not send him verification code if he tries to sign in before signing up.

I tried firebase query to compare the number user enters with the phone node to check if the number exists or not.

if (v == btn_login) {
            Toast.makeText(LoginActivity.this, "In Login Button", Toast.LENGTH_LONG).show();
            String number = et_Phone.getText().toString().trim();

            if (number.isEmpty() || number.length() < 10) {
                et_Phone.setError("Valid number is required");
                et_Phone.requestFocus();
                return;
            }
            final String ph = "+92" + number.substring(1);
            DatabaseReference ref = FirebaseDatabase.getInstance().getReference("phone");
            Query query = ref.orderByChild("phonenumber").equalTo(ph);
            query.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    if (dataSnapshot.exists()) {
                        Toast.makeText(LoginActivity.this, ph.toString(), Toast.LENGTH_LONG).show();
                        sendVerificationCode(ph);
                        L1.setVisibility(View.GONE);
                        L2.setVisibility(View.VISIBLE);


                    } else {
                        et_Phone.setError("Please enter the number again");
                        et_Phone.setText("");
                        et_Phone.requestFocus();
                    }
                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {

                }
            });


        }

Firebase Query returns null every time

数据库结构在这里

Your phone node has only a single property phonenumber . To find the property with the value you entered, use:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference("phone");
Query query = ref.orderByValue().equalTo("+923234022022");

To use orderByChild("phonenumber") the location that you're querying needs to have multiple child nodes, each of which in turn has a phonenumber property. So something like:

users: {
  uidOfUser1: {
    name: "Sikandar Niaz",
    phonenumber: "5557273456"
  },
  uidOfUser2: {
    name: "Frank van Puffelen",
    phonenumber: "5554159410"
  }
}

Now in the above structure you can use orderByChild() to find the child nodes that have a specific phone number:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference("users");
Query query = ref.orderByChild("phonenumber").equalTo("5557273456");

query.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        for (DataSnapshot userSnapshot: dataSnapshot.getChildren()) {
            System.out.println(userSnapshot.getKey())
            System.out.println(userSnapshot.child("name").getValue(String.class))
        }
    }
    ...

This will print:

uidOfUser1

Sikandar Niaz

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM