Thursday, 1 June 2017

How do I get child of a snap without knowing the key in firebase and firebase-admin

I have a queue in my database that looks like this:

server
  queue
    -RANDOM_ID_1234
      active: "true"
      text: "Some text"
    -RANDOM_ID_5678
      active: "false"
      text: "Another text"
    -RANDOM_ID_91011
      active: "false"
      text: "Text that does not matter"

I want to query and get the active item:

queueRef.orderByChild('active').equalTo('true').once('value', function(snap) {
  if (snap.exists()) {
    console.log(snap.val());
  }
});

console.log will return something like:

{
  -RANDOM_ID_1234: {
      active: "true"
      text: "Some text"
  }
}

How do I get the text without knowing the key?

I used lodash (see my answer below), but there must be a better way of doing this.



via Daniel Budick

No comments:

Post a Comment