1

Firebase structure:

{
  "config" : [
    {
      "config1" : {
        "hideimage" : true
      }
    },
    {
      "config2" : {
        "hideimage" : false
      }
    }
  ]
}

Database rules:

"config": {
  ".indexOn": ["hideimage"]
}

I'm trying to retrieve all config items that have hideimage attribute set to true using:

 admin.database().ref('config').orderByChild("hideimage").equalTo(true).once('value', result => {});

The expected result should be:

[{
  "config1" : {
    "hideimage" : true
  }
}]

but I'm retrieving a null response without getting any error.

0

1 Answer 1

2

Your data structure contains two nested levels:

  1. you have an array
  2. inside the first array element, you have config1 and config2

You can see this if you look at the Firebase console, where your data will show like:

{
  "config" : {
    "0": {
      "config1" : {
        "hideimage" : true
      }
    },
    {
      "config2" : {
        "hideimage" : false
      }
    }
  }
}

Firebase can only query nodes in a flat list, not a tree. So with your current data structure it can only find the node with hideimage=true under /config/0, not under all /config children.

Since you're already naming your config1 and config2 uniquely, I think the array may be a mistake, and you're really looking for:

{
  "config": {
    "config1" : {
      "hideimage" : true
    },
    "config2" : {
      "hideimage" : false
    }
  }
}

With this data structure your query will work.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.