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

383
Vistas
How the use for each in java script google-cloud-function?

With this code

 import * as functions from "firebase-functions";
import admin from "firebase-admin";
import { addFeed } from "../../../utils/feeds";

export default functions.firestore
  .document("meinprofilsettings/{userUid}/following/{followingUid}")
  .onCreate(async (snap, context) => {
    const db = admin.firestore();
    const { userUid, followingUid } = context.params;
    const userSnap = await db.doc(`meinprofilsettings/${userUid}`).get();
    const { username = "Userdoesnotexists" } = userSnap.exists
      ? userSnap.data()
      : 7;


      //code for the each
      const uservideos= await db.collection("videos").where(“uid”, “==“, followingUid).get().then(function(video){
        await db
        .doc(`meinprofilsettings/${followingUid}/followingvideos/${userUid}`)
        .set({ uid: userUid ,videourl:video.data.videourl}, { merge: true });
      }),



    const incrementFollowing = admin.firestore.FieldValue.increment(1);

    return db
      .doc(`meinprofilsettingscounters/${userUid}`)
      .set({ following: incrementFollowing }, { merge: true });
  });

Im trying to get from a collection every videos that are from a specific user. Then I wanna set in another collection each id of the video with the video url ,but im struggling with the code .

So with this part

  const uservideos= await db.collection("videos").where(“uid”, “==“, followingUid).get()

Im getting each video. im sure that this is corect , but how can now set every video in another collection

like this

.then(function(video){
        await db
        .doc(`meinprofilsettings/${followingUid}/followingvideos/${userUid}`)
        .set({ uid: userUid ,videourl:video.data.videourl}, { merge: true });
      }),

In this part im struggling and not sure what to do exactly and if its correct. So please hope anyone can tell me if we can use a ".then". If so , how can I ran then a foreach and how can I get the data like video url or name . If it's not possible, please tell me then how to do it also.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Since you want to execute an undetermined number of calls to the asynchronous set() method in parallel, you can use Promise.all() as follows:

export default functions.firestore
  .document("meinprofilsettings/{userUid}/following/{followingUid}")
  .onCreate(async (snap, context) => {
      
    const db = admin.firestore();
    const { userUid, followingUid } = context.params;
    const userSnap = await db.doc(`meinprofilsettings/${userUid}`).get();
    
    const { username = "Userdoesnotexists" } = userSnap.exists
      ? userSnap.data()
      : 7;

    const videosQuerySnapshot = await db.collection("videos").where(“uid”, “==“, followingUid).get();
    
    const promises = [];
    
    videosQuerySnapshot.forEach(videoDocSnapshot => {
        
        promises.push(db
            .doc(`meinprofilsettings/${followingUid}/followingvideos/${userUid}`)
            .set({ uid: userUid, videourl: videoDocSnapshot.get('videourl') }, { merge: true })
        )
        
    })
    
    await Promise.all(promises);

    const incrementFollowing = admin.firestore.FieldValue.increment(1);

    return db
      .doc(`meinprofilsettingscounters/${userUid}`)
      .set({ following: incrementFollowing }, { merge: true });
  });
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