java script google-cloud-function 中的每个函数如何使用?
How the use for each in java script google-cloud-function?
使用此代码
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 });
}),
在这部分我很纠结,不确定到底该做什么以及它是否正确。所以请希望有人能告诉我我们是否可以使用“.then”。如果是这样,我如何 运行 然后是 foreach 以及如何获取视频 url 或名称等数据。如果不行,请告诉我怎么做。
因为你想执行 未确定数量 对 异步 set()
方法的调用 parallel,你可以这样使用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 });
});
使用此代码
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 });
}),
在这部分我很纠结,不确定到底该做什么以及它是否正确。所以请希望有人能告诉我我们是否可以使用“.then”。如果是这样,我如何 运行 然后是 foreach 以及如何获取视频 url 或名称等数据。如果不行,请告诉我怎么做。
因为你想执行 未确定数量 对 异步 set()
方法的调用 parallel,你可以这样使用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 });
});