【问题标题】:how to trigger a firestore collection via cloud function如何通过云功能触发 Firestore 集合
【发布时间】: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


【解决方案1】:

我找出了导致错误“querySnapshot.forEach 不是函数”的原因。根据to this answer,您需要先查询集合,因为get() 返回的是文档而不是快照。这是一个示例代码(见第 3 步):

// 1) Create followed users posts ref
const followedUserVideosCollection = admin
    .firestore()
    .collection("videos")
    .doc("Videos 1") // I changed the value with your sample for test purposes and also because I'm not sure how you fill up this doc.
    .collection("userVideos");

// 2) Create following user's timeline ref
const timelineVideosCollection = admin
    .firestore()
    .collection("timeline")
    .doc(userfollowerId)
    .collection("timelinePosts");

// 3) Get followed users posts & Add each user post to following user's timeline
await followedUserVideosCollection.where('id', '==', 0).get().then((querySnapshot) => {
    if (querySnapshot) {
        querySnapshot.forEach(doc => {
            if (doc) {
                const videoId = doc.id;
                const videoData = doc.data();
                timelineVideosCollection.doc(videoId).set(videoData);
            }
        });
    }else {
        console.log("Document not found");
    }
}).catch((error) => {
    console.log(error);
});

一种解决方案是创建一个过滤器,并确保您要查找的文档与该过滤器匹配。例如,子集合 userVideos 中的文档应该有一个名为 id 的字段,其值为 0。

您可能必须改造您的数据库以修复我发表评论的行,但此代码应写入 timeline 集合。

【讨论】:

  • 非常感谢,即使我通过添加带有 userId 的用户视频子集合来修改视频集合后,代码也没有触发时间线集合。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-10-26
  • 2020-10-30
  • 2019-04-14
  • 1970-01-01
  • 1970-01-01
  • 2020-01-09
  • 1970-01-01
相关资源
最近更新 更多