I trigger a Cloud function for Firebase at certain time. This function is fired properly.
When the function still have to execute, in my scenario it is possible that the data where the function will write is deleted main while.
So after the data is deleted, a new path with the .update() data is created.
How can I avoid that behavior? Is there some function to call to stop a certain function?
Here is a sample of my code. The situation right now is about two functions:
- Get a Facebook long lived token, wich fires when a user loged in at first, only then Firebse gives a Facebook accesstoken :( It looks like:
exports.getFacebookLongLivedAccessToken = functions.database.ref('/users/{uid}/...').onWrite(event => {
const accessToken = event.data;
if(event.data.val() !== null) {
const baseFBUrl = "https://graph.facebook.com/oauth/access_token?client_id=...&client_secret=...&grant_type=fb_exchange_token&fb_exchange_token="
const fbAccessTokenUrl = baseFBUrl + accessToken.val();
axios.get(fbAccessTokenUrl)
.then(function (response) {
return accessToken.ref.parent.update({accessTokenLongLived: response.data.access_token});
})
.catch(function (error) {
console.error(error);
return accessToken.ref.parent.update({accessTokenLongLived: error + ' - ' + fbAccessTokenUrl});
});
} else {
return;
}
});
The second function I mentioned about, is a user deletion, this looks like:
exports.cleanupUserData = functions.auth.user().onDelete(event => { const uid = event.data.uid; console.error(uid) console.error(event.data) return admin.database().ref('/users/' + uid).remove(); //return uid.ref.parent.remove(uid); });
I know; the change of logged in and afterwards a deletion is not zo big, but I tend to make more functions and get more used and so on. So for me it is important to understand and get this right.