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

440
Vistas
How to order a firebase collection and then write something to each document?

Hey I am new to firebase cloud functions, and I want to order a collection, which is stored in firestore, by a field and then write in each document of the collection the 'position' (this should happen every 60 minutes). After reading the documentations and some suggestions from Stackoverflow I tried following approach.

exports.scheduledFunctionPosition = functions.pubsub.schedule('every 60 minutes').onRun((context) => {
    return admin.firestore().collection("example")
    .orderBy("number", "desc")
    .get()
    .then(querySnapshot => {
      if (!querySnapshot.empty) {
        for(let i =0;i<querySnapshot.size;i++){

          var queryDocumentSnapshotTmp = querySnapshot.docs[i];
          

          queryDocumentSnapshotTmp.ref.update({'position':i+1});

        }
        
  
        return null;
  
      } else {
       console.log("");
        return null;
      }
    });

As my programming skills aren't too good, I tried it the easy way, but I think there is a problem with the asynchronous programming. This code works perfect for the documents with the highest numbers. But when there are too much documents, this function won't update the last documents. Is there a solution for this?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

The .update() is also asynchronous - you likely need to wait for it, whether using .then() or switching to async/await. My personal style would write it like:

exports.scheduledFunctionPosition = functions
  .pubsub
  .schedule('every 60 minutes')
  .onRun(async (context) => {
  //will be returning a PROMISE
  let QuerySnapshot = await admin.firestore().collection("example")
    .orderBy("number", "desc")
    .get();
  
  return QuerySnapshot.empty
    ? (console.log(""), Promise.resolve(null))
    : Promise.all( //Promise.all waits for all promises in an array to complete
      //returns an array, but we use it for the array of promises
      QuerySnapshot.docs.map(async (doc, index) => {
        return doc.ref.update({'position':index+1}) //returns a promise
      });
  });

Returning the Promise makes sure the function waits for operations to complete.

Also note Cloud Functions have a hard time limit - default is 60 seconds. They can time out if you have too many records.

over 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