我可以在Firebase函数上实现beforeCreate触发器吗 [英] Can I implement beforeCreate trigger on Firebase Functions

查看:72
本文介绍了我可以在Firebase函数上实现beforeCreate触发器吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Firebase文档中,他们提到了4种类型触发器:

On the Firebase docs they mention 4 types of triggers:

  • onCreate
  • onDelete
  • onUpdate
  • onWrite

有没有一种方法可以在触发"child_added"侦听器之前侦听Cloud Functions中的添加行并修改添加行的字段?有没有一种方法可以实现BeforeCreate?

Is there a way to listen to added row in the Cloud Functions and modify fields of an added row before the "child_added" listeners are triggered? Is there a way to implement BeforeCreate?

所需的BeforeCreate周期(在Cloud Functions中):

Desired BeforeCreate cycle (in Cloud Functions):

  • 请求添加新消息
  • 更改消息字段
  • 添加具有已修改字段的新邮件
  • 客户收到"child_added"事件

推荐答案

在提交已提交后, Cloud Functions中实时数据库的所有事件均会异步触发.因此,其他用户可能已经在功能更改之前看到了数据.

All events for the Realtime Database in Cloud Functions trigger asynchronously after the write has been committed. For this reason, other users may already have seen the data before your function can change it.

要解决此问题,您将要确保仅将数据写入经过验证/修改后的每个人看到的 位置.

To solve this problem you'll want to ensure the data only gets written to the location everyone sees after it's been validated/modified.

要在数据的侦听器看到新数据之前对其进行验证/修改,您有两种选择:

To validate/modify the new data before listeners to that data can see it, you have two options:

  1. 使用HTTP触发函数来写入数据.应用程序代码调用HTTP函数,该函数执行所需的数据处理,然后将结果写入数据库.

  1. Use a HTTP triggered function for writing the data. The application code calls the HTTP function, which does the data manipulation you want, and then writes the result to the database.

让应用程序写入审核队列",该队列只是数据库中的一个单独位置. 云功能"从该队列触发,验证/修改数据,将其写入实际位置,然后将其从队列中删除.

Have the applications write to a "moderation queue", which is just a separate location in the database. The Cloud Function triggers fro this queue, validates/modifies the data, writes it to the actual location, and then deletes it from the queue.

使用这两种方法,您都会失去部分Firebase实时数据库的透明脱机行为,因此您必须选择.

With both of these approaches you lose parts of the transparent offline behavior of the Firebase Realtime Database though, so you'll have to choose.

这篇关于我可以在Firebase函数上实现beforeCreate触发器吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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