I want to write cloud function for Firestore which will decrement task.elapsedCount and make user.points += 3 in two parallel threads:
exports.taskDoneFunc = functions.firestore
.document('/tasks/{taskId}')
.onUpdate(event => {
const taskRef = event.data.ref;
const root = taskRef.root;
return taskRef.get()
.then(taskSnap => {
let task = taskSnap.data();
task.elapsedCount -= 1;
taskRef.set(task, {merge: true})
return root.document('/users/${task.taskOwner_uid}').get()
}).then(userSnap => {
let user = userSnap.data();
user.points += 3;
return userSnap.ref.set(user)
})
});
first Promise works fine and I see changes in task Firestore "task" document, but second says "Cannot read property 'document' of undefined".
I've not found any clean examples for Cloud Functions for Firestore database triggers, this is crazy Beta? :(