Firebase: How to query by key?

16,784

Solution 1

If you know the key, you can use the child method.

However, the more fundamental problem is that the value is not directly available from the ref. You can obtain the value using the once method - to listen for the value event - which returns a promise that resolves to a snapshot:

function testing(req, resp, next) {
  database.ref('locations').child('050PNS74ZW8XZ')
    .once('value')
    .then(function(snapshot) {
      var value = snapshot.val();
      console.log('location:', value.account_capabilities);
      resp.json(value.account_capabilities);
    })
    .catch(next);
}

There is more information in the documentation.

Solution 2

if you know the key you can access the child directly, then turn it into a object with $firebaseObject.

var locref = database.ref('locations').child('050PNS74ZW8XZ');
const location = $firebaseObject(locref);

the equalTo function should be used when querying from a list.

Share:
16,784
bigpotato
Author by

bigpotato

Updated on June 04, 2022

Comments

  • bigpotato
    bigpotato about 2 years

    I have a locations table that I want to query. The key is "050PNS74ZW8XZ".

    enter image description here

    As you can see, it exists.

    However, when I print out any attributes it doesn't work

    function testing(req, resp) {
      const location = database.ref('locations').equalTo('050PNS74ZW8XZ');
      console.log('location:', location.account_capabilities); // <--- prints undefined
      resp.json("asfdsf");
    }
    

    What am I doing wrong???