How do I get the key "-KLpcURDV68BcbAvlPFy" when I know the field "name" contains "efg" in the following structure in Firebase.
clubs
-KLpcURDV68BcbAvlPFy
dept: "abc"
desc: "xyz"
name: "efg"
-asdasdasddsad
dept: "asda"
desc: "asd"
name: "adddd"
I tried this but it returned "clubs"
mDatabase.child("clubs").orderByChild("name").equalTo("efg").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String clubkey =dataSnapshot.getKey();
Firebase automatically creates API keys for your project when you do any of the following: Create a Firebase project > Browser key auto-created. Create a Firebase Apple App > iOS key auto-created. Create a Firebase Android App > Android key auto-created.
In Firebase Database everything is a node, that follows the pattern key: value. Firebase Database provides us with a simple way to generate unique keys. Unique keys create new items while uploading data to a previously stored key will update.
You can create a unique key in Firebase database using push() and then add child nodes under that key. Now next time when you come to that activity, first check if parent node exists or not. If the node exists, save parent node key and use it to save new child nodes.
The key elements of Firebase database list handling are the push() method, the Query class, child and value event listeners and a range of filtering options.
To get the key from firebase you can try following in your code: itemsRef.observe (.value, with: { snapshot in for child in snapshot.children { if let alldata = child as?
What are the “Child Key Values”. The “Child Key Values” are defined as that type of the values, where the various types of the sub-values are been calculated respectively with the help of the tokens which are used in the respective “Firebase Database” respectively. III.
Be sure your Firebase project is still selected. Click Create credentials > API key. Take note of the new API key, then click Restrict key. In the API restrictions section, select Restrict key, then add to the list only the Super Service API. This new API key grants access only to the Super Service API.
Secure your database and Cloud Storage data by using Firebase Security Rules, not by restricting and/or obscuring your API keys. A Firebase project can have many API keys, but each API key can only be associated with a single Firebase project. Firebase automatically creates API keys for your project when you do any of the following:
If anyone needs to do this using Kotlin:
mDatabase.child("clubs")
.orderByChild("name")
.equalTo("efg")
.addListenerForSingleValueEvent(object: ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
dataSnapshot.children.forEach {
//"it" is the snapshot
val key: String = it.key.toString()
}
}
override fun onCancelled(p0: DatabaseError) {
//do whatever you need
}
})
That's because you're using a ValueEventListener
. If the query matches multiple children, it returns a list of all those children. Even if there's only a single matches child, it's still a list of one. And since you're calling getKey()
on that list, you get the key of the location where you ran the query.
To get the key of the matches children, loop over the children of the snapshot:
mDatabase.child("clubs")
.orderByChild("name")
.equalTo("efg")
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
String clubkey = childSnapshot.getKey();
But note that if you assume that the club name is unique, you might as well store the clubs under their name and access the correct one without a query:
mDatabase.child("clubs")
.child("efg")
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String clubkey = dataSnapshot.getKey(); // will be efg
firebaseDatabase = FirebaseDatabase.getInstance();
mReference = firebaseDatabase.getReference("clubs");
mReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot ds: dataSnapshot.getChildren()){
String key = ds.getKey();
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With