Get child object - firebase

0

I have a firebase database in this structure:

{
  "-L4Wqs3YbAlUgTWElF4Q" : {
    "receita" : {
      "-L6m_C46-Mj1py6RtF8H" : {
        "imagem" : "default",
        "ingrediente" : [ "ovo", "leite" ],
        "nome" : "um nome",
        "preparo" : "um preparo",
        "tipo" : true
      },
      "-L6m_Finc29fAlqU0nqe" : {
        "imagem" : "default",
        "ingrediente" : [ "amendoim" ],
        "nome" : "teste",
        "preparo" : "teste",
        "tipo" : false
      }
    },
    "senha" : "123456789",
    "usuario" : "guilherme"
  },
  "-L4WrImJ05VhzBgKHX6S" : {
    "senha" : "123456789",
    "usuario" : "patricia"
  }
}

I have users and users have recipes, how do I get recipes from all users that type is true ?

My read function:

function ler() {
    firebase.database().ref(referencia_database).orderByChild('nome').once('value').then(function(snapshot) {
        snapshot.forEach(function(childSnapshot) {
            var chave = childSnapshot.key
            var obj = childSnapshot.val()
            var url

            if(obj.imagem == './image/default.png')
                url = './image/default.png'
            else
                url = 'https://firebasestorage.googleapis.com/v0/b/receitas-alpha.appspot.com/o/' + chave + '%2F' + obj.imagem + '?alt=media&token=c634d1db-dc4c-4cb9-8630-fbedff537237'

            mostrar(chave, obj.nome, url)
        })
    })
}

referencia_database is chave_usuario_logado + '/receita/'

I want to make this query by holding the sort order by name

    
asked by anonymous 05.03.2018 / 23:48

1 answer

1

Well, let's break it down.

First: Find recipes from all users

Because of how you structured your data, you can not find all the recipes. I suggest you change the data structure to have a "recipes" node and a "users" node:

{
  "usuarios":{
    "-L4WrImJ05VhzBgKHX6S" : {
      "senha" : "123456789",
      "usuario" : "patricia"
    },
    "-L4Wqs3YbAlUgTWElF4Q" : {
      "senha" : "123456789",
      "usuario" : "guilherme"
    }
  },
  "receitas":{
      "-L6m_C46-Mj1py6RtF8H" : {
        "imagem" : "default",
        "ingrediente" : [ "ovo", "leite" ],
        "nome" : "um nome",
        "preparo" : "um preparo",
        "tipo" : true,
        "usuario":"-L4WrImJ05VhzBgKHX6S"
      },
      "-L6m_Finc29fAlqU0nqe" : {
        "imagem" : "default",
        "ingrediente" : [ "amendoim" ],
        "nome" : "teste",
        "preparo" : "teste",
        "tipo" : false,
        "usuario":"-L4WrImJ05VhzBgKHX6S"
      }
  }
}

Note that I also added a "user" attribute if you need to know who each recipe belongs to.

So, to find all recipes with type = true you would do:

firebase.database().ref('receitas').orderByChild('tipo').equalTo(true)

Second: Keeping sort by name

You mentioned that you want to keep sorting by name. Firebase does not allow sorting by 2 attributes (type and name of your case). You will have to group these 2 into a 3rd attribute (I'll call name_name) and use it to sort. This attribute will contain the type concatenated with the recipe name.

So the revenue node looks like this:

  "receitas":{
      "-L6m_C46-Mj1py6RtF8H" : {
        "imagem" : "default",
        "ingrediente" : [ "ovo", "leite" ],
        "nome" : "um nome",
        "preparo" : "um preparo",
        "tipo" : true,
        "tipo_nome":"true_um nome",
        "usuario":"-L4WrImJ05VhzBgKHX6S"
      },
      "-L6m_Finc29fAlqU0nqe" : {
        "imagem" : "default",
        "ingrediente" : [ "amendoim" ],
        "nome" : "teste",
        "preparo" : "teste",
        "tipo" : false,
        "tipo_nome":"false_teste",
        "usuario":"-L4WrImJ05VhzBgKHX6S"
      }
  }

And the code would look like:

firebase.database().ref('receitas').orderByChild('tipo_nome').startAt('true_').endAt('true_\uf8ff');

No endAt used \uf8ff because it is the highest character in the unicode range.

    
06.03.2018 / 09:45