siuhoi wong
siuhoi wong

Reputation: 1

Firebase data use unique key to find data

firebase datd

I want to use the email to find the "帳號". How can I do it?

THX

Upvotes: 0

Views: 65

Answers (2)

Ziya ERKOC
Ziya ERKOC

Reputation: 839

Do the following:

FirebaseDatabase.getInstance().getReference("user").orderByChild("put the key of your email")
.equalTo("put here the email you are looking for").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
    //dataSnapshot includes all the children whose email you wanted
    for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
        //It will give you what you want
        System.out.println(postSnapshot.getValue("帳號")
    }
}

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

Upvotes: 1

Alex Mamo
Alex Mamo

Reputation: 138824

To solve your problem i recomand you using in stead of that random generated key provided by the push() method, the email it self. To check if an email address exists, just use the exists() method directly on the dataSnapshot object.

Because Firebase does not accept . symbol in the key, i suggest you encode the email address like this:

[email protected] -> name@email,com

Hope it helps.

Upvotes: 0

Related Questions