Ad

How Can I Query Firebase For An EqualTo Boolean Parameter?

- 1 answer

My database looks like

{
  "chats" : {
    "-K5hUtxNOPLWwRTb8fAj" : {
      "clientId" : 893,
      "messages" : {
        "-K5hUtyCwWuPeFT0cSXr" : {
          "chatKey" : "-K5hUtxNOPLWwRTb8fAj",
          "messageText" : "new message",
          "timestamp" : 1450314801055,
          "userId" : "19eaa3f0-4ab3-44b4-b418-03b64ca2313a"
        },
        "-K5hUvwKsAOvuvt-ymFG" : {
          "chatKey" : "-K5hUtxNOPLWwRTb8fAj",
          "messageText" : "did this work",
          "timestamp" : 1450314809127,
          "userId" : "48f7a208-5647-4d0e-921c-c49595de1db2"
        }
      },
      "newQuestion" : false,
      "startedByUserId" : "19eaa3f0-4ab3-44b4-b418-03b64ca2313a"
    },
    "-K5hVKjs4SkSHD7WTh1I" : {
      "clientId" : 518,
      "messages" : {
        "-K5hVNzPKNgxLCgkfInI" : {
          "chatKey" : "-K5hVKjs4SkSHD7WTh1I",
          "messageText" : "this is new",
          "timestamp" : 1450314928109,
          "userId" : "19eaa3f0-4ab3-44b4-b418-03b64ca2313a"
        }
      },
      "newQuestion" : true,
      "startedByUserId" : "19eaa3f0-4ab3-44b4-b418-03b64ca2313a"
    }
  }
}

How can I get all chats that have a newQuestion set to true? I've tried a few different queries, the latest is:

firebaseDb.child("chats/newQuestion").equalTo(true).once("value"...

But that complains about an order being needed for a boolean?

Ad

Answer

You have to specify an orderBy method first.

var query = firebaseDb.child("chats").orderByChild("newQuestion").equalTo(true);

Here's the Firebase docs on querying.

Ad
source: stackoverflow.com
Ad