leodriesch
leodriesch

Reputation: 5780

Get a Firebase entry based on key

I can't find a way to get a firebase entry based on the key(marked in red on the picture). I have the key, and want to get the other attributes of the entry(blurred out here), but I can't make the correct query. Here's what I tried:

database
    .reference()
    .child("items")
    .orderByChild("key")
    .equalTo(myKey)
    .once()

enter image description here

Upvotes: 2

Views: 954

Answers (2)

Frank van Puffelen
Frank van Puffelen

Reputation: 598901

If you have the value of the key (-LJEQKe-GU7n2if4Zfyj), you can access that specific item with the code:

var key = "-LJEQKe-GU7n2if4Zfyj";
var ref = database.reference(items").child(key)
ref.once().then((snapshot) {
  print(snapshot.key);
});

Alternatively, you can use a query to match the item. The main difference is that you'll get a list of items in the snapshot when you use a query, which means you need to loop over the results:

var key = "-LJEQKe-GU7n2if4Zfyj";
var query = database.reference(items").orderByKey().equalTo(key)
query.onChildAdded.listen((snapshot) {
  print(snapshot.key);
});

Upvotes: 1

diegoveloper
diegoveloper

Reputation: 103421

You can try something like this :

    database
        .reference()
        .child("items")
        .child(myKey)
        .once()

or

         database
        .reference()
        .child("items/$myKey")
        .once()

Check here to get more information : https://firebase.google.com/docs/reference/js/firebase.database.Reference

Upvotes: 4

Related Questions