javascript - Firebase 的云函数 - 使用 Promise 循环

标签 javascript firebase firebase-realtime-database google-cloud-functions firebase-admin

我正在尝试创建一个触发器,将测试分数相加,然后根据之前的测试结果计算学生的分类情况。

我正在尝试在 FOR 循环中使用 Promise,如下所示:

exports.boxScoresUpdate = functions.database.ref('/Tests/{id}/TestScores').onWrite(event => {

    let testScr = 0;

    for (let i = 1; i <= section; i++) {
        //
        testScr += parseInt(nValue[i]);

        var index;

         admin.database().ref('TestScores').child(data.key).child('Summative').child(i).once("value").then(x => {

            xIndex = x.val();


             admin.database().ref('TestScores').child(data.key).child('Formative').child(i).once("value")
        }).then(y => { 

                yIndex = y.val();


                 admin.database().ref('StudentPlacement').child(data.key).child(xIndex + ":" + yIndex).once("value", snapshot => {

                     // SnapShot
                     console.log("Student Placement is: ", snapshot.val());

                });

        }).catch(reason => {

              // Handle Error
              console.log(reason);

        });
    }
}

有人告诉我这不会像 post 中所示那样工作。 .

"Once a promise is resolved or rejected, it forever retains that state and can't be used again. To repeat the work, I think you'd have to construct another chain of new promises representing the second iteration of work."

我一直在尝试重组我的触发器,但我无法弄清楚,我将如何构建新的 promise 链来实现我想要的结果?!有谁遇到过并解决过这个问题吗?

我希望实现的行为是使触发器迭代四 (4) 次 section 等于 4。

我需要利用 promise ,否则迭代将无法正确完成,特别是 testScr += parseInt(nValue[i]); 以及对 Summative 和 Formative 的查找。

但如上所述,使用 Promises 工作得很好,只是它只迭代第一个实例,而不是当 i = 2 或 3 或 4

最佳答案

这种方法不太干净,但可能会对您有所帮助。

exports.boxScoresUpdate = functions.database.ref('/Tests/{id}/TestScores').onWrite(event => {

  let testScr = 0;

  for (let i = 1; i <= section; i++) {
    //
    testScr += parseInt(nValue[i]);

    var index;

    admin.database().ref('TestScores').child(data.key).child('Summative').child(i).once("value").then(x => {
      xIndex = x.val();
      return { xIndex, index: i };
    }).then(({ xIndex, index}) => {
      admin.database().ref('TestScores').child(data.key).child('Formative').child(index).once("value").then(y => {
        yIndex = y.val();
        return { yIndex, xIndex };
      }).then(({ yIndex, xIndex}) => {
        admin.database().ref('StudentPlacement').child(data.key).child(xIndex + ":" + yIndex).once("value", snapshot => {
          console.log("Student Placement is: ", snapshot.val());
        });
      });        
    }).catch(reason => {
      console.log(reason);
    });
  }
});

关于javascript - Firebase 的云函数 - 使用 Promise 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45656346/

相关文章:

javascript - Google Maps Javascript - 计算航向和 compass 方向以计算面向方向

javascript - WinJS (WP8.1) : update secondary tile from background task

JQuery:选择动态创建的元素并推送到 Firebase

node.js - Firebase 异步函数语法错误

javascript - Firebase >> 用户收藏夹列表

java - Spring MVC 和表单绑定(bind) : how to remove an item from a List?

javascript - Angular 和 Laravel 应该是单独的应用程序还是 Laravel 应该为 Angular 应用程序提供服务?

firebase - Firebase Firestore中合并字段和合并字段之间的区别

ios - 无法获得对Firebase数据库IOS的许可

java - firebase 实时数据库 W/ClassMapper : No setter/field for found on class