Is the "current" status of a document equal to change.before.data() or change.after.data() when observing a Firestore document onUpdate event

huangapple go评论106阅读模式
英文:

Is the "current" status of a document equal to change.before.data() or change.after.data() when observing a Firestore document onUpdate event

问题

我有一个情况,用户可以没有、一个或多个订阅。如果用户至少有一个status == "active"的订阅,就应该被视为"已订阅"。

我使用onUpdate()触发器来观察订阅的更改。然后,我获取用户并检查他们所有的订阅,并相应地设置用户的is_subscribed属性。

我的逻辑只适用于假定订阅的更改已经在用户的subscriptions集合中进行了更新。但也许这并不是有保障的?我在文档中找不到相关信息。

英文:

I have a situation where a user can have zero, one, or multiple subscriptions. The user should be deemed "subscribed" if he has at least one subscription with status == "active".

I'm using the onUpdate() trigger to observe when a subscription changes. Then I get the user and check all of their subscriptions and set the user's is_subscribed attribute accordingly.

My logic only works under the assumption that the subscription being changed has already been updated in the user's subscriptions collection. But perhaps this isn't guaranteed? I couldn't find this in the documentation.

答案1

得分: 1

使用“current”这个术语在这里是令人困惑的。change.before是新更改之前数据库的状态。change.after是新更改之后数据库的状态。

然而,在Cloud函数执行时,更改已经提交到数据库,因为Cloud Firestore Cloud函数不会阻塞Cloud函数。另外,如果您对同一文档进行多次快速更改,change.after可能不会反映函数执行时文档的确切状态。

这意味着两者都不是“current”版本,因为它们只是特定更改之前和之后的数据的快照。一般来说,您的Cloud函数应该只对发生更改的数据进行操作,不做其他操作,这将有助于避免混淆的更新,并有助于跟踪日志的源头,以便在必要时进行审查。

在您的函数日志中,您应该记录类似于“用户#{uid}已取消订阅#{subId}”或“用户#{uid}已订阅#{subId}”的消息,然后对相关文档进行相关更新。

如果您需要在执行时精确获取数据库中的内容,请使用getDoc(change.after.ref),然后利用返回的数据。正如之前提到的,要小心可能导致数据冲突和令人困惑的行为。

英文:

Using the term "current" here is confusing. change.before is the state of the database before the new change. change.after is the state of the database after the new change.

However, by the time the Cloud Function is executed, the change has already been committed to the database as Cloud Firestore Cloud Functions are not blocking Cloud Functions. Additionally, if you make multiple changes to the same document in quick succession, the change.after may not reflect the state of the document at the exact time the function is executing.

This means neither is the "current" version, as each is just a snapshot of the data before and after that particular change. In general, your Cloud Function should act only on the data that changed and nothing else as this will prevent confusing updates and help track the source of bugs when you have to review the logs.

In the logs for your function, you should log messages similar to "User #{uid} subscription to #{subId} was marked inactive" or "User #{uid} subscribed to #{subId}" and then make the relevant updates to the relevant documents.

If you require exactly what is in the database at the time of execution, use getDoc(change.after.ref) and then make use of the returned data. As mentioned before, be warned that this may lead to data conflicts and confusing behaviour.

huangapple
  • 本文由 发表于 2023年2月27日 06:40:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75575432.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定