仅在更新字段时如何触发Firebase云功能?

时间:2018-08-27 13:39:09

标签: node.js firebase google-cloud-firestore google-cloud-functions

我只想在更新Firestore文档的特定字段时触发云功能。我不想每次文档更新时都触发它,无论哪个字段。可能吗?

例如:

exports.onAvatarUpdated = functions.firestore.document('users/{userId}/avatarUrl')
  .onUpdate(change => { 
  // do something here
});

2 个答案:

答案 0 :(得分:2)

当前没有针对特定字段的触发器。您只能触发对文档的任何更改,并且如果您想知道某个特定字段是否发生了更改,则必须检查更改对象以找出答案。

如果您想表达只关注特定领域的功能,可以随时file a feature request

答案 1 :(得分:0)

我将从 here 更改 Firebase 原生手动代码:

exports.updateUser = functions.firestore
.document('users/{userId}')
.onUpdate((change, context) => {
  // Get an object representing the document
  // e.g. {'name': 'Marie', 'age': 66}
  const newValue = change.after.data();
  const newFieldValue = newValue.field;

  // ...or the previous value before this update
  const previousValue = change.before.data();
  const previousFieldValue = previousValue.field;


  if (previousFieldValue!== newFieldValue) 
       {      // perform desired operations ... }
});