3

Function with transaction operation is timing out. I am not able find out what is wrong. I am not sure if it is an issue with firebase.

exports.ComputeUserReviews = functions.database.ref("/reviews/{userid}/{jobid}").onWrite((event) => {
  const userid = event.params.userid;
  const parentRefPromise =  admin.database().ref(`reviews/${userid}`).once('value');
  const UserReviewRef = admin.database().ref(`users/${userid}/account`);
  return Promise.all([parentRefPromise]).then(results => {
    const userReviewSnapshot = results[0];
    const hasreviewPromise = UserReviewRef.child("hasreview").transaction(current => {
      if (userReviewSnapshot.numChildren() === 0) {
          return false;
      } else if (userReviewSnapshot.numChildren() > 0) {
          return true;
      }
    });
    const reviewcountPromise =  UserReviewRef.child("reviewcount").transaction(current => {
      if (event.data.exists() && !event.data.previous.exists()) {
        return (current || 0) + 1;
      } else if (!event.data.exists() && event.data.previous.exists()) {
        return (current || 0) - 1;
      }
    });
    return Promise.all([hasreviewPromise, reviewcountPromise]);
  });
});
Diego P
  • 1,728
  • 13
  • 28
  • As per this question there is an issue with firebase, I am not sure if this issue is affecting my function http://stackoverflow.com/questions/43151022/cloud-functions-for-firebase-onwrite-timeout – Diego P Apr 26 '17 at 22:22
  • Did you try the solution in that other post? Did it work? – Doug Stevenson Apr 27 '17 at 06:54
  • 1
    No it didn't work, it is still timing out. Tried: ` const IncrementProposalJob = new Promise(function(resolve, reject) { jobRef.transaction(current => { return (current || 0) + 1; }, () => resolve(null)); }); ` – Diego P Apr 27 '17 at 12:33
  • Should we ignore this bug and wait until it is fixed? – Diego P Apr 27 '17 at 12:45
  • I'm actively using this workaround in some of my own projects. But it should be addressed eventually. – Doug Stevenson Apr 27 '17 at 18:08
  • Added `.then(()=>{console.log('message');});` and working perfectly, no more timeouts thanks https://github.com/firebase/functions-samples/issues/119 – Diego P Apr 27 '17 at 18:47

1 Answers1

0

There is an issue with the firebase SDK, as a workaround adding .then(()=>{console.log('message');}); fix the timeout.

const reviewcountPromise =  UserReviewRef.child("reviewcount").transaction(current => {
  if (event.data.exists() && !event.data.previous.exists()) {
    return (current || 0) + 1;
  } else if (!event.data.exists() && event.data.previous.exists()) {
    return (current || 0) - 1;
  }
}).then(()=>{console.log('message');});
Diego P
  • 1,728
  • 13
  • 28