I have a firebase function that goes as follows:
exports.myFunction = functions.database.ref('/users/{id}/').onWrite((snapshot, context) => {
//do some processing here
var number;
snapshot.after.ref.parent.once('value').then((newSnapshot) => {
number = newSnapshot.child('number').val();
});
console.log(number);
//do some processing with the use of var number
//returning the updates after processing
return snapshot.after.ref.update(updates);
});
I realized that the variable number is coming out to be undefined as the function that I am using to obtain the value of number is running asynchronously. However, I am unable to think of a method to convert this implementation so that it works the way I want it. Any help would be appreciated.