Let's take the following data structure:

Now I want to update accessTokenFacebook using the Firebase function. I checked two options:
OnWrite looks better to me, but with the following function:
exports.getFacebookAccessTokenOnchange = functions.database.ref('/users/{uid}/userAccountInfo/lastLogin').onWrite(event => { const lastLogin = event.data; let dateObject = new Date(); let currentDate = dateObject.toUTCString(); return lastLogin.ref.parent.parent.child('services').child('facebook').update({'accessTokenFacebook': currentDate}); });
Something is happening, I donβt understand / I can decide: when I delete the entire userUID record (for cleaning), the userUID record automatically creates, and then only with the following path: {uid} / services / facebood / accesTokenFacebook ...
Removal seems to trigger onWrite as well.
I also tried .onchange, but this one only works when there is still no access to TokenFacebook. When a change does this, the change never starts again.
So, the next thing I want to do is to compare the old and the new meaning. Do you have an example? Or is there a better solution?
source share