Firebase 如何在文档中创建自定义变量的侦听器

Firebase 如何在文档中创建自定义变量的侦听器,firebase,google-cloud-firestore,google-cloud-functions,Firebase,Google Cloud Firestore,Google Cloud Functions,此代码在文档内部发生任何更改时更新,但我希望在更改自定义变量时不更新任何变量 exports.updateUser = functions.firestore.document('Test/uhfL5NE199eYTGyfSH1srtrtee').onUpdate((change, context) => { const washingtonRef = admin.firestore().collection('Test').doc('uhfL5NE199eYTGyfSH1srtrte

此代码在文档内部发生任何更改时更新,但我希望在更改自定义变量时不更新任何变量

exports.updateUser = functions.firestore.document('Test/uhfL5NE199eYTGyfSH1srtrtee').onUpdate((change, context) => {
  const washingtonRef = admin.firestore().collection('Test').doc('uhfL5NE199eYTGyfSH1srtrtee');
  return washingtonRef.update({Counts:admin.firestore.FieldValue.increment(1)});
});
例如,我想在change
Score
变量时调用此函数

exports.updateUser = functions.firestore.document('Test/uhfL5NE199eYTGyfSH1srtrtee').onUpdate((change, context) => {
  const washingtonRef = admin.firestore().collection('Test').doc('uhfL5NE199eYTGyfSH1srtrtee');
  return washingtonRef.update({Counts:admin.firestore.FieldValue.increment(1)});
});

这是不可能的。使用Cloud函数和Firestore,当文档已存在且任何值已更改时,会触发
.onUpdate(
)(请参阅)

您可以使用两个快照,这两个快照表示触发事件之前和之后的数据状态,并且出现在
更改
对象中,如下所示:

exports.updateUser = functions.firestore.document('Test/uhfL5NE199eYTGyfSH1srtrtee').onUpdate((change, context) => {

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

  //Check if the Score field has changed
  if (newValue.Score  !== previousValue.Score) {

    //Score field has changed! -> Do whatever you want

  } else {
     //End the Cloud Function
     return false;
  }


});

我能知道什么是变量名改变了吗?好的,我感谢你的帮助,谢谢。