Home > OS >  How the use for each in java script google-cloud-function?
How the use for each in java script google-cloud-function?

Time:11-30

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.

CodePudding user response:

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 });
  });
  • Related