Pick up child object - firebase

Asked

Viewed 1,074 times

0

I have a firebase bank 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 to get recipes from every user that type true?

My reading 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 keeping the order by name

  • What is the structure of snapshot ?

1 answer

1


Well, let’s go in pieces.

First: Find all users' recipes

The way you structured your data, you can’t 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:

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

Second: maintaining order by name

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

Then the recipes knot would look 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 be:

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

In the endAt I used the \uf8ff because it is the highest character in the Unicode interval.

  • Could I do what I asked without changing the data structure? Just out of curiosity, I will change my structure

  • I don’t think so. This is the best way I see.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.