使用给定的uid首次插入Cloud Firestore文档时触发Firebase Cloud Function 数据结构:云功能代码:假设:数据结构:云功能代码:

我想使用给定的uid首次在插入Cloud Firestore文档时触发Cloud Funtion

下面的代码在插入newUserId时触发

#wrapper,.container-inner {
    min-width: 320px;
    overflow: hidden;
}

要求-

  • 文档:发布
  • 字段:id,标题,uid

id是文档ID。

在这里,如果是uid(user)的第一篇文章,则应该触发Cloud函数。

zhihao222 回答:使用给定的uid首次插入Cloud Firestore文档时触发Firebase Cloud Function 数据结构:云功能代码:假设:数据结构:云功能代码:

编辑后的回复(在评论中进行了澄清):

数据结构:

users/{userId}/posts/
 ◙ post18sfge89s
   - title: My first post!
   - t: 1572967518
 ◙ post2789sdjnf
   - title: I like posting
   - t: 1572967560

posts/
 ◙ post18sfge89s
   - title: My first post!
   - uid: someUid1
   - comments/
     ◙ comment237492384
       ...
     ◙ comment234234235
       ...
 ◙ post2789sdjnf
   - title: I like posting
   - uid: someUid1

云功能代码:

使用上述结构,您将需要两个Cloud Functions来管理它-一个用于处理每个新帖子(将信息复制到作者的帖子列表),另一个用于检查它是否是作者的第一篇帖子。

// Function: New post handler
exports.newPost = functions.firestore
  .document('posts/{postId}')
  .onCreate((postDocSnap,context) => {

    // get relevant information
    const postId = postDocSnap.id; // provided automatically
    const postTitle = postDocSnap.get('title');
    const userId = postDocSnap.get('uid');
    const postedAt = postDocSnap.createTime; // provided automatically

    // add to user's post list/index
    return firestore.doc(`users/${userId}/posts/${postId}`)
      .set({
        title: postTitle,t: postedAt
      });
  });

// Function: New post by user handler
exports.newPostByUser = functions.firestore
  .document('users/{userId}/posts/{postId}')
  .onCreate((postDocSnap,context) => {

    // get references
    const userPostsColRef = postDocSnap.ref.parent;
    const userDocRef = userPostsCol.parent;

    // get snapshot of user data
    return userDocRef.get()
      .then((userDocSnap) => {

        // check if "First Post Event" has already taken place
        if (userDocSnap.get('madeFirstPostEvent') != true) {
          return getCollectionSize(userPostsColRef).then((length) => {
            if (length == 1) {
              return handleFirstPostByUser(userDocSnap,postDocSnap);
            } else {
              return; // could return "false" here
            }
          });
        }
      });
  });

// Pseudo-function: First post by user handler
function handleFirstPostByUser(userDocSnap,postDocSnap) {
  return new Promise(() => {
      const postId = postDocSnap.id;
      const postTitle = postDocSnap.get('title');
      const userId = userDocSnap.id;

      // do something

      // mark event handled
      return userDocSnap.ref.update({madeFirstPostEvent: true});
    });
}

// returns a promise containing the length of the given collection
// note: doesn't filter out missing (deleted) documents
function getCollectionSize(colRef) {
  return colRef.listDocuments()
     .then(docRefArray => {
       return docRefArray.length;
     });
}


原始回复(针对每个团队的私人帖子):

假设:

  • 检查用户在特定团队而非平台范围内的第一条帖子。
  • 未知的数据结构-我已经使用了我认为可以与您现有的结构很好配合的数据。

数据结构:

teamContent/集合的结构使其可以包含帖子,附件,图片等不同项目的子集合。

teamProfile/{teamId}/teamMemberList/{userId}/posts/
 ◙ post18sfge89s
   - title: My first post!
   - t: 1572967518
 ◙ post2789sdjnf
   - title: I like posting
   - t: 1572967560

teamContent/{teamId}/posts/
 ◙ post18sfge89s
   - title: My first post!
   - author: someUid1
   - comments/
     ◙ comment237492384
       ...
     ◙ comment234234235
       ...
 ◙ post2789sdjnf
   - title: I like posting
   - author: someUid1

云功能代码:

使用上述结构,您将需要两个Cloud Function来对其进行管理–一个用于处理每个新帖子(将信息复制到作者的帖子列表中),另一个用于检查它是否是该特定帖子中作者的第一篇帖子团队。

// Function: New team post handler
exports.newPostInTeam = functions.firestore
  .document('teamContent/{teamId}/posts/{postId}')
  .onCreate((postDocSnap,context) => {

    // get relevant information
    const postId = postDocSnap.id; // provided automatically
    const postTitle = postDocSnap.get('title');
    const authorId = postDocSnap.get('author');
    const postedAt = postDocSnap.createTime; // provided automatically
    const teamId = context.params.teamId;

    // add to user's post list/index
    return firestore.doc(`teamProfile/${teamId}/teamMemberList/${authorId}/posts/${postId}`)
      .set({
        title: postTitle,t: postedAt
      });
  });

// Function: New post by team member handler
exports.newPostByTeamMember = functions.firestore
  .document('teamProfile/{teamId}/teamMemberList/{userId}/posts/{postId}')
  .onCreate((postDocSnap,context) => {

    // get references
    const userPostsColRef = postDocSnap.ref.parent;
    const userDocRef = userPostsCol.parent;

    // get snapshot of user data
    return userDocRef.get()
      .then((userDocSnap) => {

        // check if "First Post Event" has already taken place
        if (userDocSnap.get('madeFirstPostEvent') != true) {
          return getCollectionSize(userPostsColRef).then((length) => {
            if (length == 1) {
              return handleFirstPostInTeamEvent(userDocSnap,postDocSnap);
            } else {
              return; // could return "false" here
            }
          });
        }
      });
  });

// Pseudo-function: First post by team member handler
function handleFirstPostInTeamEvent(userDocSnap,postDocSnap) {
  return new Promise(() => {
      const postId = postDocSnap.id;
      const postTitle = postDocSnap.get('title');
      const userId = userDocSnap.id;

      // do something

      // mark event handled
      return userDocSnap.update({madeFirstPostEvent: true});
    });
}

// returns a promise containing the length of the given collection
// note: doesn't filter out missing (deleted) documents
function getCollectionSize(colRef) {
  return colRef.listDocuments()
     .then(docRefArray => {
       return docRefArray.length;
     });
}


注意:

  • 以上代码并非完全idempotent。如果一次上传来自同一用户的多个帖子,则handleFirstPost*函数可能会被多次调用。
  • 以上代码未解决getCollectionSize()函数中的missing documents returned from listDocuments()。在上面的示例中,这不是问题,因为{userId}/posts集合没有任何子集合。如果您在其他地方叫它,请提防。
  • 不包括错误处理
  • 使用async / await语法可以使其更简洁
  • 以上代码是在未部署的情况下编写的,可能存在错误/错别字
本文链接:https://www.f2er.com/3158830.html

大家都在问