是否可以通过回调的快照参数访问 Cloud Firestore 触发器上集合的子集合?

Is it possible to access the subcollection of the collection on Cloud Firestore triggers via the callback's snapshot parameter?

比方说,我需要在群组完成后向群组的所有成员发送一封电子邮件:

export const sendEmailToNewMember = functions.firestore.document('group/{groupId}').onCreate(async (snapshot,context) => {
}

集合 group 有子集合 members。我检查成员的计数是否增加并与可以加入组的成员总数相匹配。如果最后一个成员加入了,我需要给所有成员发一封邮件,说这个组已经完成了。

数据结构如下所示:

collection group {
  membersCount: 9999,
  totalMembers: 100000,
  ....

  users subcollection
}

我试过像这样通过 ref 访问集合:

const usersRef = snap.ref.collection('users)

但是,结果是undefined。现在,我使用

访问它
await firestore().collection('group').doc(groupId).collection('users')

是否可以在不等待子集合的情况下从 snapshot 本身访问子集合?

document() 采用文档而非字段的路径。因此,如果您想在组文档中的任何字段更新时触发此功能,请将路径设置为 .document('group/{groupId}').

此外 snapshot in onCreate() 将仅包含该组文档的数据(触发该功能的文档)。您必须明确阅读 sub-collection.

您似乎想向加入群组的新成员发送电子邮件。您可以改为在用户 sub-collection 中添加新用户时触发该功能,如下所示:

// Here user's UID would be ID of sub-document
export const sendEmailToNewMember = functions.firestore.document('group/{groupId}/users/{userId}').onCreate(async (snapshot,context) => {
  const { groupId, userId } = context.params;
}