由子文档或集合完成创建时,firestore OnCreate不会触发 [英] firestore OnCreate not triggered when creation is done by a sub document or collection

查看:60
本文介绍了由子文档或集合完成创建时,firestore OnCreate不会触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在使用Flutter和Firebase构建一个聊天应用程序,每当两个新用户首次开始彼此聊天时(无论其中一个发送第一条消息时),我都需要触发一个功能,以便我可以存储该信息稍后再将其显示为用户的聊天记录(已与之联系的人)的地方.

So I am building a chat app using flutter and firebase and I need to trigger a function whenever two new users starting chatting with each other for the first time (whenever one of them sent the first message) so I can store that information somewhere to show it later to the users as chat history (people who've got in contact with).

所以我的数据树看起来像这样:'rooms/{roomId}/messages/{messageWithRandomId}'

So my data tree looks like this : 'rooms/{roomId}/messages/{messageWithRandomId}'

从逻辑上讲,每当用户向其他用户发送第一条消息时,就会创建会议室文件以及包含单个消息文档(带有randomId)的子集合消息". `

Logically whenever a user sends the first message to another user the room document gets created along with the sub-collection 'messages' containing a single message document with a randomId. `

我已经在会议室的集合上设置了一个OnCreate侦听器,每当我在会议室"下手动创建新文档时,就会触发该监听器.

I've set up an OnCreate listener on the room's collection and it gets triggered whenever I manually create a new document under 'rooms'.

但是当子集合创建相同的文档时,不会触发该事件.

But it doesn't get triggered when that same document is created by the subcollection.

所以我的代码如下:

export const testfunction = functions.firestore.document('rooms/{_someRoom}').onCreate(async (snapshot,context) => {

// the function Core
 }

推荐答案

当文档创建相同的文档时,不会触发该文档 子集合.

It doesn't get triggered when that same document is created by the subcollection.

这是正常现象.

事实上,如果直接在messages集合下创建具有完整路径rooms/{roomId}/messages/{messageWithRandomId}的文档,则将不会创建任何中间文档(即没有roomId文档)

As a matter of fact, if you create a document directly under a messages collection with the full path rooms/{roomId}/messages/{messageWithRandomId}, no intermediate documents will be created (i.e. no roomId document).

所以,当你说:

room文档与子集合一起创建 messages包含带有randomId

the room document gets created along with the sub-collection messages containing a single message document with a randomId

,如果仅使用randomId创建了消息文档,则实际上没有创建room文档,因此不会触发云功能.

, if you only created the message document with a randomId, there is actually no room document created and, consequently, the Cloud Function is not triggered.

Firebase控制台以斜体显示roomId会议室文档"作为一种容器"(或占位符"),以便具体化"层次结构并允许您导航到messageWithRandomId邮件文档,但会议室文档在Firestore数据库中不存在.

The Firebase console shows in italics the roomId room "document" as a kind of "container" (or "placeholder"), in order to "materialize" the hierarchy and to allow you to navigate to the messageWithRandomId message document, but the room document doesn't exist in the Firestore database.

让我们举一个更通用的例子:想象一个col1集合下的doc1文档

Let's take a more generic example: Imagine a doc1 document under the col1 collection

col1/doc1/

和subCol1(子)集合下的另一个subDoc1

and another one subDoc1 under the subCol1 (sub-)collection

col1/doc1/subCol1/subDoc1

实际上,从技术的角度来看,它们根本不相关.这样做的副作用是,如果删除文档,则该文档的子集合仍然存在.

Actually, from a technical perspective, they are not at all relating to each other. They just share a part of their path but nothing else. One side effect of this is that if you delete a document, its sub-collection(s) still exist.

这意味着您应该:

rooms集合下创建roomId文档

OR

通过以下方式触发您的云功能:

trigger your Cloud Function with:

export const testfunction = functions.firestore.document('rooms/{_someRoom}/messages/{_someMessage}').onCreate(async (snapshot,context) => {
  //.....   
}

这篇关于由子文档或集合完成创建时,firestore OnCreate不会触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆