【发布时间】:2021-05-13 10:08:25
【问题描述】:
每当我在我的应用中单击以下按钮时,我想从现有的关注者收藏和视频收藏中触发一个新的收藏(时间线收藏)。
现在的问题是,Cloud Function 是从视图日志中创建的,但不会创建新的集合(时间线集合)。
下面是 Cloud Function 的代码,我在其中定位关注者集合和视频集合以创建新的时间线集合。我期待您的帮助。
const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();
// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//
// exports.helloWorld = functions.https.onRequest((request, response) => {
// response.send("Hello from Firebase!");
// });
exports.onCreateFollower = functions.firestore
.document("/followers/{userId}/userFollowers/{userfollowerId}")
.onCreate(async (snapshot, context) => {
console.log("The Event has Created The Follower", snapshot.id);
const userId = context.params.userId;
const userfollowerId = context.params.userfollowerId;
// 1) Create followed users posts ref
const followedUserVideosCollection = admin
.firestore()
.collection("videos")
.doc(userId)
.collection("userVideos");
// 2) Create following user's timeline ref
const timelineVideosCollection = admin
.firestore()
.collection("timeline")
.doc(userfollowerId)
.collection("timelinePosts");
// 3) Get followed users posts
const querySnapshot = await followedUserVideosCollection.get();
// 4) Add each user post to following user's timeline
querySnapshot.forEach(doc => {
if (doc.exists) {
const videoId = doc.id;
const videoData = doc.data();
timelineVideosCollection.doc(videoId).set(videoData);
}
});
});
【问题讨论】:
-
从提供的代码中,我看不到 /videos/{userId}/userVideos 的写入位置。你能补充一下发生这种情况的地方吗?
-
我试图从关注者集合中定位用户 followerID,即如果我关注用户,我应该能够看到他或她在我的时间线集合中发布的所有视频,所以我定位从followers 集合中获取followersID 以从视频集合中获取其所有视频,即followersID 发布到视频集合的所有视频。希望你能理解。请帮我。云函数已创建但未创建时间线集合。谢谢。
-
每当触发函数时,您是否在 Cloud Functions 日志中看到任何错误?
-
我没有注意到您提供的图片。我现在尝试编辑问题。
-
好的,等待.....
标签: javascript java node.js flutter google-cloud-firestore