您能帮我吗?-firebase-function-onUpdate

时间:2019-10-08 07:02:33

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

我想使用^-?\d{1,8}.?\d{1,2}$ 函数在“ front”更新时触发,但是不幸的是我无法使其正常工作。你能帮我吗?

enter image description here

onUpdate

1 个答案:

答案 0 :(得分:1)

您应该在更改前后比较front字段的值,如下所示:

exports.detectFrontChanges = functions.firestore
.document('trashcan/{trashcanId}')
.onUpdate((change, context) => {

  const newValue = change.after.data();
  const previousValue = change.before.data();

  if (newValue.front !== previousValue.front) {
     //front field value has changed
     //Do something

     //e.g. write to the log console
     console.log("front field value has changed!");
     //e.g. and write a Firestore document
     return admin.firestore().collection('events').doc('event1').set({changeType: 'front field'});
  } else {
     //nothing to do
     //return null to indicate to the Cloud Function platform that this Function can be finished
     return null;
  }
}