FRestore文档onUpdate:仅针对特定字段触发

2023-12-08

在我的云功能中,我有下一个功能:

export const collectionOnUpdate = functions.firestore.document('cards/{id}').onUpdate(async (change, context) => {
  await updateDocumentInAlgolia(change);
});

每次编辑文档的任何字段时都会触发此功能。但我只想在特定字段之一更改时运行它(在本例中,例如:title, category)而不是其他任何情况。

我怎样才能做到这一点?


firebase 火库cloud functions是基于文档更改的作品,而不是基于文档的字段。

所以如果你想检查某些字段是否有变化,你必须手动检查

exports.updateUser = functions.firestore
.document('users/{userId}')
.onUpdate((change, context) => {

  // ...the new value after this update
  const newValue = change.after.data()||{};

  // ...the previous value before this update
  const previousValue = change.before.data()||{};

  // access a particular field as you would any JS property

  //The value after an update operation
  const new_name = newValue.name;

  // the value before an update operation
  const old_name = previousValue.name;

  if(new_name!==old_name){
   //There must be some changes
   // perform desired operations ...
  }else{
  //No changes to the field called `name`
  // perform desired operations ...
  }


});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

FRestore文档onUpdate:仅针对特定字段触发 的相关文章

随机推荐