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

181
Visualizações
Check if Key has Key in Firebase - JS

I have a Firebase Realtime DB structured like this:

users
    userid1
        name: "name1"
        email: "email1"
        specialValue: "special"
    userid2
        name: "name2"
        email: "email2"

I want to check if a user(I already have a userid) has the specialValue key. So this should return true for userid1, and false for userid2.

I have tried this:

var check = firebase.database().ref('users').orderByKey().equalTo(userid).once("value", function (snapshot) {
    if (snapshot.exists()) {
        console.log("key exists");
    } else {
        console.log("key doesn't exist");
    }
});
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You don't need a query for a node if you know the key, so this should work (and be a bit faster once you have a lot of nodes):

firebase.database().ref('users').child(userid).once("value").then((snapshot) => {
    if (snapshot.exists()) {
        console.log("exists");
    } else {
        console.log("doesn't exist");
    }
});

In addition to being shorter and faster, this also is guaranteed to give just one snapshot as a result, where a query may have multiple results. Even though your query will only match one result, the API still returns the one result as a list and you'd have to loop over that with forEach.

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