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

130
Vistas
Trouble batch setting a document field by docId in Firestore

I have been using firebase (firestore) for a while but I'm a little stuck and was wondering if anyone can think of a solution.

On the firestore DB I have a single collection of users, each user has an email address and several other fields. In this instance I am checking if a user email exists and if it does, I want to create a list field for that particular user with a listUid. I am referencing the users by email, grabbing the docId for those users and then trying to set a list field for each of them.

I am not getting any error's from firestore, it's simply not updating in the DB for some reason and I can't figure out where I am going wrong. Thanks in advance

export const addListUidToExistingUserList = (
  { firestore },
  emailArray,
  listUid
) => {
  return async () => {
    let docIds = [];

    emailArray.forEach((emailAddress) => {
      //find users by email (works)
      const query = db
        .collection("users")
        .where("email", "==", emailAddress);

      //get docId's for user with matching email (works)
      query.get().then((querySnapshot) => {
        querySnapshot.forEach((doc) => {
          docIds.push(doc.id);
        });
      });

      //add a new list with corresponding listUid (does not work)
      docIds.forEach((id) => {
        let userRef = db.collection("users").doc(id);
        batch.set(userRef, { lists: [{ listUid }] });
      });
    });
    return await batch.commit();
  };
};
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You are running into this issue because your docIds array is always empty at the time you call docIds.forEach.

That's because query.get().then runs asynchronously, and so docIds.forEach is not waiting for it to complete.

You could either:

  • await query.get().then; or
  • Add the docIds.forEach function INSIDE the then callback of query.get.

Here are your possible fixes:

  1. await query.get().then
//get docId's for user with matching email (works)
await query.get().then((querySnapshot) => {
  querySnapshot.forEach((doc) => {
    docIds.push(doc.id);
  });
});

OR:

  1. docIds.forEach inside then
//get docId's for user with matching email (works)
query.get().then((querySnapshot) => {
  querySnapshot.forEach((doc) => {
    docIds.push(doc.id);
  });

  docIds.forEach((id) => {
    let userRef = db.collection("users").doc(id);
    batch.set(userRef, { lists: [{ listUid }] });
  });
});

Note: Of course, you could also add batch.set directly into your first iteration of querySnapshot.docs.forEach to prevent an unnecessary iteration.

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