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

查看:14
本文介绍了由子文档或集合完成创建时未触发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.

所以我的代码看起来像这样:

So my Code looks like this :

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 文档,因此不会触发 Cloud Function.

, 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/

还有另外一个subDoc1在subCol1(子)集合下

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 文档

Create yourself the roomId document under the rooms collection

触发您的云函数:

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

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

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