Get Value from Firebase Database

Josh

I need to get the string value from the node passcode in my Firebase database to compare with a user input, but unfortunately I am not able to get the value. This is the link to my firebase database in the below image.

Firebase Database screenshot

This is my codes below:

 final DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference("pin_code");

        mDatabase.addListenerForSingleValueEvent(new com.google.firebase.database.ValueEventListener() {
            @Override
            public void onDataChange(com.google.firebase.database.DataSnapshot dataSnapshot) {
                String rface = (String) dataSnapshot.child("pincode").getValue();
                if (rface.equals(userPassword) && !rface.equals("")){
                    Intent intent = new Intent(PinActivity.this, ProfileActivity.class);
                    startActivity(intent);
                }
                else {
                    if (rface.equals("") || rface.equals(null)){
                        // Creating new user node, which returns the unique key value
                        // new user node would be /users/$userid/
                        String userId = mDatabase.push().getKey();

                        // creating user object
                        Pin pin = new Pin(authUserId, userPassword);

                        mDatabase.child(userId).setValue(pin);

                        Intent intent = new Intent(PinActivity.this, ProfileActivity.class);
                        startActivity(intent);
                    }
                    else {
                        Toast.makeText(PinActivity.this,"Invalid PIN code", Toast.LENGTH_SHORT).show();
                        return;
                    }

                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
        });

This is the json code

  {
  "pin_code" : {
"id" : "TQYTo1NHNnhPJnOxhe1Vok3U6ic2",
"pincode" : "12345"
     }
  }
Frank van Puffelen

This FirebaseDatabase.getInstance().getReference("pin_code") does not refer to the node you're looking for. Most likely you know the id property, in which case you can get the node with:

DatabaseReference collection = FirebaseDatabase.getInstance().getReference("p...");
Query query = collection.orderByChild("id").equalTo("TQT...ic2");

query.addListenerForSingleValueEvent(new com.google.firebase.database.ValueEventListener() {
    @Override
    public void onDataChange(com.google.firebase.database.DataSnapshot dataSnapshot) {
      for (DataSnapshot child: dataSnapshot.getChildren()) {
        String rface = (String) child.child("pincode").getValue();
        if (rface.equals(userPassword) && !rface.equals("")){

The changes I made:

  1. On the first line we get the collection: the node under which you want to run a query. You struck out the name of that node in the screenshot, but it's the second line you marked.
  2. In the second line we create a query on the id property of each child node under the collection.
  3. In the onDataChange we added a loop. This is needed because a query against the Firebase Database will potentially have multiple results. So the dataSnapshot contains a list of those results. Even if there is only a single result, the snapshot will contain a list of one result. We loop over dataSnapshot.getChildren() to handle those multiple results.

If there can ever only be one node with the same id, you should consider changing your data structure to use the id as the key of the node. So:

pin_codes
  uid1: "pincode1"
  uid2: "pincode2"

Your code then becomes significantly simpler, because you don't need to query for the user anymore. You can just directly read from the path:

DatabaseReference user = FirebaseDatabase.getInstance().getReference("pin_codes").child("TQT...ic2");

user.addListenerForSingleValueEvent(new com.google.firebase.database.ValueEventListener() {
    @Override
    public void onDataChange(com.google.firebase.database.DataSnapshot dataSnapshot) {
        String rface = (String) dataSnapshot.getValue();
        if (rface.equals(userPassword) && !rface.equals("")){

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Get value from Firebase Database Object

Swift Get Specific Value From Firebase Database

How to get a value from database firebase

How to get specific value from firebase database?

Android, get value from firebase database

Firebase functions how to get a boolean value from Firebase Database

Get value from JSON data from Firebase Database

How to get Access to this nested value from Firebase Database in a String Variable?

How to get data based on value from firebase database

Trying To Get Key From a Child Value In Firebase Database

Android Studio - How to get value from FireBase Realtime Database

Get the value of a specific child from the result of a query in a Firebase database in Javascript

How i get child value from firebase database in clicked item

how to get second last value from firebase database in web javascript?

How to read only one key to get value from Firebase Database?

Want to get value from Javascript into HTML form to submit to Firebase database

How to get value from firebase realtime database in android?

How to get a specific value from firebase realtime database using kotlin

Android Firebase Database get value

Removing a value from Firebase Database

remove a value from firebase database

Firebase database how to get some value from 1 reference and equals to 2 reference to get its keys and values

Spinner from database - get value

How to get value from database

TCPDF get value from database

Can not get a value from database

How to get only changed value on Firebase Database?

Get Firebase Database Value into a Cloud Function

How to get value in Firebase Database (Angular) by key?