Is my function not resolving correctly? Also it won't write to firebase to the databse. No errors

情到浓时终转凉″ 提交于 2021-01-29 11:25:19

问题


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!


回答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



来源:https://stackoverflow.com/questions/56800763/is-my-function-not-resolving-correctly-also-it-wont-write-to-firebase-to-the-d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!