【问题标题】:Is my function not resolving correctly? Also it won't write to firebase to the databse. No errors我的功能没有正确解析吗?它也不会将firebase写入数据库。没有错误
【发布时间】:2019-06-28 04:03:48
【问题描述】:

我有一个运行没有错误的云函数,但是在 Promise.all 之后它不会写入数据库

我已经在网络浏览器中测试了这个功能,并且在 Promise.all 之后数据似乎可以正确解析。当我将它放入云函数时,Promise.all 之后的 console.log() 会显示原始数据而不是更新的数据,就好像它正在跳过 forEach 并立即解析一样。当函数在浏览器中运行时,我没有得到这种行为。

最重要的是,如果在 Promise 之后,数据将不会写入 Firestore。在此之前写入的所有数据都正常。

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");

      });
    });
   });

如有任何帮助,我们将不胜感激!

【问题讨论】:

    标签: javascript firebase google-cloud-firestore google-cloud-functions


    【解决方案1】:

    您似乎在尝试使用 getIndex 时仍然不可用,请查看异步操作...使用 async/await 您可以重组代码以使其看起来像

    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(/.../)
    

    这样你就强迫你的执行等到承诺得到解决

    【讨论】:

    • 说真的,谢谢。我在这方面有点菜鸟,像你这样的人真的对像我这样的新手有很大的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-16
    • 1970-01-01
    • 1970-01-01
    • 2014-01-25
    • 2020-04-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多