Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

283
Visualizações
Pass a variable into a query (Firebase)

this is really frustrating me I hope you kind people can assist me.

I have a firebase firestore db that stores info about properties and cities. All properties contain a key:value pair called location_city. It is a string that stores the City that the property resides in.

I have made a dropdown box on a webpage with a list of available cities. The idea is to dynamically display properties from the selected city, by means of the .where() method. The third parameter accepted by the method is the string you want to query against.

I want to pass the text value of the dropdown list into the query.

Here's my code:

let selectedCityViewHomes;

function getDropdownSelection(sel) {
  selectedCityViewHomes = sel.options[sel.selectedIndex].text;
  selectedCityViewHomes.toString();
}

db.collection('Homes')
  .where('location_city', '==', selectedCityViewHomes)
  .get()
  .then(snapshot => {
    snapshot.docs.forEach(doc => {
      renderHome(doc);
    });
  });

The query method only seems to work if you manually enter a string into the parameter and does not seem to allow you to pass a variable (which is a string) into the method.

I have tried concatenating "'" either side to mimic 'Manchester', for example and this did not work either.

TIA

Charlie

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

EDIT: I found where i had gone wrong, it was an issue of scope i think. I fixed it by creating a function which wrapped the db.collection code and passed in the variable as an argument.

const updateHomesViewHomes = (input) => {
    db.collection('Homes').where('location_city', '==', input).get().then((snapshot) => {
        homesViewHomes.innerHTML = "";
        snapshot.docs.forEach(doc  => {
            renderHome(doc);
        });
    });
};

Thanks for the replies, hope this helps someone.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda