I return transaction promise which should wait for transaction to finish before stopping the function. The transaction executes fine, but the promise seems to never resolve.
I see in the Firebase console that this function always times out after 60s.
const functions = require('firebase-functions');
const admin = require("firebase-admin");
const db = admin.database();
export let countFollowers = functions.database.ref('followers/{followee}/{follower}').onWrite(event => {
const followee = event.params.followee;
let path = `posts/${followee}/cnt_foll`;
const countRef = db.ref(path);
let out = countRef.transaction(current => {
if (event.data.exists() && !event.data.previous.exists()) {
return (parseInt(current) || 0) + 1;
} else if (!event.data.exists() && event.data.previous.exists()) {
return (parseInt(current) || 0) - 1;
}
});
return out;
});
EDIT:
I solve the problem with the following "hack", I create a promise myself, because whatever .transaction
is returning is not working:
return new Promise(function(resolve, reject) {
countRef.transaction(current => {
if (event.data.exists() && !event.data.previous.exists()) {
return (parseInt(current) || 0) + 1;
} else if (!event.data.exists() && event.data.previous.exists()) {
return (parseInt(current) || 0) - 1;
}
}, () => resolve(null));
});