如何使用java脚本中的每一个谷歌云函数



使用此代码

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

我试图从一个集合中获取来自特定用户的每一个视频。然后我想在另一个集合中设置视频的每个id和视频url,但我很难处理代码。

所以有了这个部分

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 });
}),

在这一部分,我很挣扎,不确定该做什么,以及是否正确。所以请希望有人能告诉我我们是否可以使用";。那么";。如果是这样,我如何运行foreach,以及如何获取视频url或名称等数据。如果不可能的话,请告诉我怎么做。

由于要并行执行对异步set()方法未确定数量的调用,因此可以按如下方式使用Promise.all()

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

相关内容

  • 没有找到相关文章

最新更新