Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

197
Vistas
Firebase Realtime database query from v8 to v9 SDK

I have a Firebase Realtime database query written using Firebase version 8 that I'm looking to migrate over to the v9 SDK.

export const getSingleDataWithQuery = async ({ key, email, criteria }) => {
  if (!criteria) return;
  const snapshot = await realTimeDb
    .ref()
    .child(key)
    .orderByChild(query)
    .equalTo(criteria)
    .get();
  const val = snapshot.val();
  if (val) {
    const keys = Object.keys(val);
    return val[keys[0]];
  }
  return null;
};

In this example:

  • key would be the 'users' collection
  • the email field is looking for users by their email
  • and the criteria is the user's actual email (jane.doe@gmail.com)

Using Firebase's Read data once and Sorting data documentation I managed to narrow it down to perhaps being this, but I'm not sure if it's correct:

export const getSingleDataWithQuery = async ({ key, query, criteria }) => {
  if (!criteria) return;
  const dbRef = query(ref(realTimeDb, key), orderByChild(email), equalTo(criteria));
  get(dbRef).then(snapshot => {
    if (snapshot.exists()) {
      const val = snapshot.val();
      if (val) {
        const keys = Object.keys(val);
        return val[keys[0]];
      }
    }
  });
  return null;
};
about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Aside from the fact that you may have swapped query and email in the fragments, the only difference is in the way you handle the asynchronous database call and that likely explains the difference. Since you use then in the second snippet, the function doesn't actually return a promise and so calling it with await it in the caller won't have any effect.

To make those the same again, use await in the second snippet too, instead of then:

export const getSingleDataWithQuery = async ({ key, query, criteria }) => {
  if (!criteria) return;
  const dbRef = query(ref(realTimeDb, key), orderByChild(email), equalTo(criteria));
  const snapshot = await get(dbRef); // 👈
  if (snapshot.exists()) {
    const val = snapshot.val();
    if (val) {
      const keys = Object.keys(val);
      return val[keys[0]];
    }
  }
  return null;
};
about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda