Node.js firebase函数中的onCreate函数未在云数据库中创建所需的集合

Node.js firebase函数中的onCreate函数未在云数据库中创建所需的集合,node.js,firebase,flutter,google-cloud-firestore,google-cloud-functions,Node.js,Firebase,Flutter,Google Cloud Firestore,Google Cloud Functions,我刚刚在函数firebase CLI的index.js文件中键入了一个代码。根据我的代码,必须在firebase的云数据库中创建一个时间轴集合。函数运行正常,部署时没有错误,即使在日志中,一切都正常。但当我在我的应用程序中跟踪某个用户时,仍然没有在云数据库中创建时间线集合 这是我的代码: const functions = require("firebase-functions"); const admin = require("firebase-admin&quo

我刚刚在函数firebase CLI的index.js文件中键入了一个代码。根据我的代码,必须在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);
      }
    });
  });

由于您希望并行执行可变数量的异步调用,因此应使用,以等待所有这些不同的异步调用完成,然后再向CF平台指示它可以清理CF。有关更多详细信息,请参阅

exports.onCreateFollower = functions.firestore
  .document("/followers/{userId}/userFollowers/{followerId}")
  .onCreate(async (snapshot, context) => {
    
    const userId = context.params.userId;
    const followerId = context.params.followerId;

    // ...

    // 3) Get followed users posts
    const querySnapshot = await followedUserPostsRef.get();

    // 4) Add each user post to following user's timeline
     const promises = [];
     querySnapshot.forEach(doc => {
        //query results contain only existing documents, the exists property will always be true and data() will never return 'undefined'.
        const postId = doc.id;
        const postData = doc.data();
        promises.push(timelinePostsRef.doc(postId).set(postData));
    });

    return Promise.all(promises);

  });

由于您希望并行执行可变数量的异步调用,因此应使用,以等待所有这些不同的异步调用完成,然后再向CF平台指示它可以清理CF。有关更多详细信息,请参阅

exports.onCreateFollower = functions.firestore
  .document("/followers/{userId}/userFollowers/{followerId}")
  .onCreate(async (snapshot, context) => {
    
    const userId = context.params.userId;
    const followerId = context.params.followerId;

    // ...

    // 3) Get followed users posts
    const querySnapshot = await followedUserPostsRef.get();

    // 4) Add each user post to following user's timeline
     const promises = [];
     querySnapshot.forEach(doc => {
        //query results contain only existing documents, the exists property will always be true and data() will never return 'undefined'.
        const postId = doc.id;
        const postData = doc.data();
        promises.push(timelinePostsRef.doc(postId).set(postData));
    });

    return Promise.all(promises);

  });

即使使用了这个Promise.all方法,问题仍然存在。它不是在数据库中创建集合。我已经测试了我的代码,并确认它确实有效。你在云函数控制台中看到任何错误吗?对不起,先生,这是我的错误,我刚刚重新部署了我的函数,现在你的代码正在工作。一切都安排好了。感谢您的帮助。即使使用了这个Promise.all方法,问题仍然存在。它没有在数据库中创建集合。我已经测试了我的代码,并确认它确实有效。你在云函数控制台中看到任何错误吗?对不起,先生,这是我的错误,我刚刚重新部署了我的函数,现在你的代码正在工作。一切都安排好了。谢谢你的帮助。