我的功能无法正确解析吗?同样,它也不会写入Firebase到数据库.没有错误 [英] Is my function not resolving correctly? Also it won't write to firebase to the databse. No errors

查看:36
本文介绍了我的功能无法正确解析吗?同样,它也不会写入Firebase到数据库.没有错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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.

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

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!

推荐答案

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

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

这篇关于我的功能无法正确解析吗?同样,它也不会写入Firebase到数据库.没有错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆