My google cloud function is triggered when an update takes place on a document in my firestore database. The update would happen from a string being added/removed to an array in the database. How do I get the exact value added/removed to the database in the cloud function?
// Update event attendance
exports.updateEventAttendance = functions.firestore
.document('users/{userId}')
.onUpdate((change, context) => {
const newValue = change.after.data();
const oldValue = change.before.data();
const newEvents = newValue.eventsAttended;
const oldEvents = oldValue.eventsAttended;
// We'll only update if the eventsAttended has changed.
// This is crucial to prevent infinite loops.
if (newEvents === oldEvents) return null;
const newCount = newEvents.length;
const oldCount = oldEvents.length;
var db = admin.firestore()
if (newCount > oldCount) {
// Event added
// Get event id
// GET STRING THAT WAS ADDED TO THE DATABASE AND DO SOMETHING WITH IT
} else if (oldCount > newCount) {
// Event removed
// Get event id
// GET STRING THAT WAS REMOVED FROM DATABASE AND DO SOMETHING WITH IT
}
return null;
});