0

I have a cloud function that runs with no errors, however, it won't write to the database after Promise.all

I've tested this function in a web browser and the data seems to resolve correctly after the Promise.all. When I put this into a cloud function a console.log() after Promise.all shows the original data and not the updated data, as if it's skipping the forEach and resolving right away. I don't get this behavior when the function runs in a browser.

On top of that, the data will not write to Firestore if it's after the Promise.all any data write before that goes through just fine.

exports.scheduledIndexUpdate = functions.pubsub
  .schedule("every 30 minutes")
  .onRun(context => {
    console.log("This will be run every 30 minutes!");

    var newIndex;
    var getIndex = new Promise((resolve, reject) => {
      admin
        .firestore()
        .collection("billsIndex")
        .doc("GS2019Index")
        .get()
        .then(doc => {
          if (doc.exists) {
            newIndex = doc.data();
            resolve(newIndex);
          }
        });
    });
    return getIndex.then(index => {
      var keys = Object.keys(index);

      keys.forEach(function(key) {
        admin
          .firestore()
          .collection("billsMetaData")
          .doc(key)
          .get()
          .then(doc => {
            if (doc.exists) {
              const metaData = doc.data();
              let agree = 0,
                disagree = 0,
                neutral = 0,
                somewhatAgree = 0,
                somewhatDisagree = 0;

              if (metaData.votes) {
                if (metaData.votes.agree) {
                  agree = metaData.votes.agree;
                }
                if (metaData.votes.disagree) {
                  disagree = metaData.votes.disagree;
                }
                if (metaData.votes.neutral) {
                  neutral = metaData.votes.neutral;
                }
                if (metaData.votes.somewhatAgree) {
                  somewhatAgree = metaData.votes.somewhatAgree;
                }
                if (metaData.votes.somewhatDisagree) {
                  somewhatDisagree = metaData.votes.somewhatDisagree;
                }

                newIndex[key].userVotes =
                  agree + disagree + neutral + somewhatAgree + somewhatDisagree;
              }
            }
          });
      });
      Promise.all(keys).then(function(result) {

          admin
            .firestore()
            .collection("billsIndex")
            .doc("GS2019Index2")
            .set({
              newIndex
            });
          console.log(newIndex);
          console.log("done");

      });
    });
   });

Any help would be appriciated!

Onyoursix
  • 109
  • 9

1 Answers1

1

It seems you are trying to use getIndex when it is still not available, take a look into async operations... using async/await you can restructure you code to look something like

exports.scheduledIndexUpdate = functions.pubsub
  .schedule("every 30 minutes")
  .onRun(async context => {
    console.log("This will be run every 30 minutes!");

    var newIndex;
    var getIndex = await admin.collection("billsIndex")
                                .doc("GS2019Index")
                                .get()
                                .then(doc => doc.exists ? doc.data() : null);
    // you have you docs available here 
    for (let prop of getIndex) {
        await admin
          .firestore()
          .collection("billsMetaData")
          .doc(key)
          .get()
          .then(() => { /..,/ })
          // etc
    }

    await admin.firestore().collection("billsIndex")
      .doc("GS2019Index2")
            .set({
              newIndex
            });
    return Promise.resolve(/.../)

That way you are forcing your execution to wait until promise are resolved

andresmijares
  • 3,658
  • 4
  • 34
  • 40
  • Seriously, thank you. I'm kind of a noob at all this and people like you seriously are such a major help to the rookies like me. – Onyoursix Jun 28 '19 at 06:26