云功能陷入无限循环 [英] Cloud Function stuck in an infinite loop

本文介绍了云功能陷入无限循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

exports.addNewValue = functions.database.ref('/path')
.onWrite(event => {    
    event.data.adminRef.update({"timeSnapshot":Date.now()})})

似乎Date.now()导致函数中的无限循环,因为以下情况不会导致

It appears that Date.now() causes an infinite loop in the function because the following does not:

exports.addNewValue = functions.database.ref('/path')
.onWrite(event => {    
    event.data.adminRef.update({"timeSnapshot":"newString"})})

我该如何解决?

推荐答案

如果您写回数据库中以前更改过的相同位置,则可能会发生以下事件序列:

If you write back to the same location in the database that was previously changed, you can expect this sequence of events:

  1. 从客户端的第一次更改触发功能
  2. 函数写回数据库
  3. 由于在步骤2中进行写操作,第二次触发了功能

所有与过滤器路径匹配的写操作,即使来自同一函数中的所有写操作,也会触发该函数.

All writes to the database that match the filter path, even those from within the same function, will trigger the function.

在第3步中,您需要一种策略来确定第二个函数调用是否应导致再次写回数据库.如果不需要其他写操作,则该函数应及早返回,以免触发其他写操作.通常,您可以查看传递给函数的事件中的数据,并确定它们是否已被第一次修改.这可能涉及查看数据库中是否设置了某些标志,或者您修改的数据是否不需要更多更改.

In step 3, you need a strategy to figure out if the second function invocation should result in yet another write back to the database. If it does not require another write, the function should return early so that it won't trigger another write. Typically you look at the data in the event passed to the function and figure out if it was already modified the first time. This could involve looking to see if some flag is set in the database, or if the data you modified does not need any more changes.

Firebase团队提供的许多代码示例都可以做到这一点.特别要注意的是文本审核.另外,还有一个描述问题的视频和可能的解决方案.最后,您有责任提出满足您需求的策略.

Many of the code samples provided by the Firebase team do this. In particular, look at text moderation. Also, there is a video that describes the problem and a possible solution. In the end, you're responsible for coming up with the strategy that meets your needs.

这篇关于云功能陷入无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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