Dew*_*wey 10 node.js firebase google-cloud-functions google-cloud-firestore
我想仅在更新 firestore 文档的特定字段时触发云功能。我不想在每次更新文档时触发它,而不管哪个字段。是否可以?
例如:
exports.onAvatarUpdated = functions.firestore.document('users/{userId}/avatarUrl')
.onUpdate(change => {
// do something here
});
Run Code Online (Sandbox Code Playgroud)
我将从这里更改 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 ... }
});
Run Code Online (Sandbox Code Playgroud)