How to query by passing an array per parameter in the firestore?

Asked

Viewed 129 times

0

I have a list of documents in the firestore, called shopping. Within each purchase of these, I have a FIELD, called condicaoPagamento, this keeps the condition of payment of the purchase. What I need now is to provide the user with the possibility to retrieve all documents that contain a certain payment condition. So far so good, the problem comes next.

This filter, it gives the possibility to the user, to choose more than one payment condition to query, let’s assume for example: the person can choose, check, cash and card.

So I have to take this array of options that she chose, and query this firestore shopping list, where I need to compare the matrix that was provided by the user, with the fields that are inside.

I saw in the documentation, that it is possible to compare a field with a firestore matrix, but what I need is the reverse, I need to compare an array with the firestore fields, someone has ever done this ?

Follow my code thingy of how I’m trying to do this, but to no avail so far.

carregarComprasFiltrosCondicoesPagamento() {
this.compras = [];
this.db.collection("usuarios_adm_proprietarios")
.where("uidUsuario", "==", this.uidUsuario)
  .get().then((querySnapshot) => {
    querySnapshot.forEach((doc) => {
      this.idEmpresa = doc.data().idEmpresa;
      let ref = this.db.collection("empresas")
        .doc(this.idEmpresa)
        .collection("compras")
        .where("status", "==", "concluida")
        .where("condicaoPagamento", "array-contains", this.condicoesPagamento)
        .orderBy("dataEmissaoISOString", "desc")
        .limit(this.limite);
      ref.onSnapshot((querySnapshot) => {
        this.ultimaCompraCarregada = querySnapshot.docs[querySnapshot.docs.length - 1];
        querySnapshot.forEach((doc) => {
          this.compras = querySnapshot.docs.map(d => d.data());
          this.calcularTotalCompras();
        });
      })
    });
  })
  .catch((error) => {
    console.log("Error getting documents: ", error);
  });
}

I get the filter options array from my users this way:

inserir a descrição da imagem aqui

My documents in the firestore, record the payment condition this way:

inserir a descrição da imagem aqui

1 answer

0

let filtro = [] // Filtro com os parâmetros selecinados

filtro.map((element) => {
    firebase.firestore().collection("pedidos").where("condicaoPagamento", "==", element)
        .get()
        .then((querySnapshot) => {

            let retornofiltro = [] // array final com os dados filtrados do firestore 
            querySnapshot.forEach((doc) => {
                retornofiltro.push(doc.data())
            })
            conso.log()
        })
        .catch((error) => {
            console.log("Error getting documents: ", error);
        })

        conso.log(retornofiltro)

})

Browser other questions tagged

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