【问题标题】:Google Cloud Functions version >= 1 check if node value has changedGoogle Cloud Functions 版本 >= 1 检查节点值是否已更改
【发布时间】:2018-04-16 06:09:38
【问题描述】:

如何检查特定节点是否发生了变化?

event.data.child("chatid").changed()

我找不到任何关于此的文档。我正在关注这个example

change.after.changed('subscribedToMailingList')

并在控制台中收到错误:

change.after.changed 不是函数

exports.ObserveProposals = functions.database.ref("/proposals/{jobid}/{talentid}").onWrite((change, context) => {
      const jID = context.params.jobid;
      const tID = context.params.talentid;
      let jR = admin.database().ref(`/jobs/${jID}`);
      const promises = [];
      if (change.after.exists() && change.after.changed('chatid') && change.after.child("chatid").val()) return null;
      ...

if语句出现错误

【问题讨论】:

  • 请编辑您的问题以显示未按您期望的方式工作的代码(不仅仅是一行)。
  • 你试过比较“之前”和“之后”吗? change.after.val().subscribedToMailingList != change.before.val().subscribedToMailingList
  • 我已经更新了我的问题
  • @AarónBC。我更喜欢使用改变的方法
  • 嗯,看v1的迁移指南,对象类型好像从DeltaSnapshot变成了DataSnapshot。 DeltaSnapshot 是具有 changed() 方法的类型。见:firebase.google.com/docs/functions/…

标签: firebase firebase-realtime-database google-cloud-functions


【解决方案1】:

云函数documentation for database triggers 说:

对于onWriteonUpdate 事件,第一个参数是Change 对象,其中包含两个快照,表示触发事件之前和之后的数据状态。

如果你查找definition for the Change object,它有:

用于更改状态的事件的函数接口,例如实时数据库或 Cloud Firestore onWrite 和 onUpdate。

属性

afternon-null Object

表示事件发生后的状态。

beforenon-null Object

表示事件之前的状态。

它们都没有类似于之前的changed() 的方法。因此,您确实必须自己比较 beforeafter 数据,看看发生了什么变化。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-24
    • 2011-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-11
    • 2013-08-11
    相关资源
    最近更新 更多