【发布时间】:2020-10-16 20:11:04
【问题描述】:
我刚刚在我的 index.js 函数文件 (firebase CLI) 中键入了一个代码。根据我的代码,必须在 firebase 的云数据库中创建一个时间线集合。函数运行正常,并且没有错误被部署并且即使在日志中一切正常。但是当我在我的应用中关注用户时,仍然不会在云数据库中创建时间线集合。
这是我的代码:
const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();
exports.onCreateFollower = functions.firestore
.document("/followers/{userId}/userFollowers/{followerId}")
.onCreate(async (snapshot, context) => {
console.log("Follower Created", snapshot.id);
const userId = context.params.userId;
const followerId = context.params.followerId;
// 1) Create followed users posts ref
const followedUserPostsRef = admin
.firestore()
.collection("posts")
.doc(userId)
.collection("userPosts");
// 2) Create following user's timeline ref
const timelinePostsRef = admin
.firestore()
.collection("timeline")
.doc(followerId)
.collection("timelinePosts");
// 3) Get followed users posts
const querySnapshot = await followedUserPostsRef.get();
// 4) Add each user post to following user's timeline
querySnapshot.forEach(doc => {
if (doc.exists) {
const postId = doc.id;
const postData = doc.data();
return timelinePostsRef.doc(postId).set(postData);
}
});
});
【问题讨论】:
标签: node.js firebase flutter google-cloud-firestore google-cloud-functions