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

182
Vistas
Unable to check whether a field exists in a nested collection Firebase v9

Sorry I'm still new to this, but I'm trying to check whether a specific field (docName) value already exists in a nested collection to perform as a condition. I'm new to v9 and I'm slowly working my way around it, but I've looked all over and I'm still unable to get the appropriate solution.

Ref:

'teachers/${uid}/contracts/autoDocID'

What I have tried

const Contract = async (data) => {
    const q = query(collection(db, "teachers", data.uid, "contracts"), where("docName", "==", `${data.docName}`)); 
    const condition = await getDocs(q).exists;

    if(!condition){
        //adding new doc
    }
}
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

The await getDocs(q) returns a QuerySnapshot object, which doesn't have an exists property.

Are you looking to check if the snapshot is empty?

about 4 years ago · Juan Pablo Isaza Denunciar

0

I was able to do it like this:

const Contracts => async(data) = {
    const docRef = doc(db, "teachers", data.uid);
    const colRef = collection(docRef, "contracts");
    const q = query(collection(db, "teachers", data.uid, "contracts"), where("docName", "==", `${data.docName}`));
    const check = await getDocs(q);

    if(!check){
        await addDoc(colRef, {
            expiration: data.expiration,
            docName: data.docName,
            docUrl: data.docUrl,
        });
    } else {
        console.log('already exists')
    }
}
about 4 years ago · Juan Pablo Isaza 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