在使用Firebase数据库中的嵌套列表创建复杂查询时需要帮助

时间:2019-05-21 06:56:08

标签: javascript firebase react-native firebase-realtime-database

我有一个复杂方案,可从包含多个子列表的表中获取数据,并且还使用firebase数据库相互依赖。 我试图从其中具有GameResults列表的联赛表中获取数据,我想显示所有GameResults,但在GameResults中,如果DivisonID名为“ Game”,则有一个引用,并且我也想从GameSchedule列表中显示StartDate住在联赛桌里。我不是火力专家,这就是为什么搞砸了。 附上Db结构图:

我正在使用Firebase作为db在React Native中进行开发并遇到此问题。 这是我之前尝试过的代码。

await firebase
      .database()
      .ref('/Leagues/' + League_ID + '/GameResults')
      .on('child_added', snapshot => {
        const collection = snapshot.val();
        if (collection) {
          var objGameResults = null;
          const TeamIDsList = [];
          var matchdate = null;
          TeamIDsList.push(collection.TeamA);
          TeamIDsList.push(collection.TeamB);
          const LeaguePromises = TeamIDsList.map(TeamID => {
            return firebase
              .database()
              .ref()
              .child('Teams')
              .orderByKey()
              .equalTo(TeamID)
              .once('value', snapshot => snapshot);
          });
          Promise.all(LeaguePromises)
            .then(Teams => {
              Teams.forEach(singleObject => {
                var TList = JSON.stringify(singleObject);
                if (TList) {
                  var Team_List = JSON.parse(TList);
                  Object.keys(Team_List).forEach(key => {
                    if (key == collection.WinnerTeam) {
                      Team1Name = Team_List[key].TeamName;
                    } else {
                      Team2Name = Team_List[key].TeamName;
                    }
                  });
                }
              });
              GameResults.push({
                Scoredate: matchdate, //matchdate,
                ScoreData: {
                  winnerTeam: {
                    label: Team1Name,
                    value: 123,
                    division: 'South Division',
                    ps: collection.TeamAScore,
                  },
                  loserTeam: {
                    label: Team2Name,
                    value: 123,
                    division: 'South Division',
                    ps: collection.TeamBScore,
                  },
                },
              });

              this.setState({
                GameResults,
                isLoading: false,
              });
              // do something with the data
            })
            .catch(err => {
              // handle error
              this.setState({ isLoading: false });
            });
        }
      });


await firebase
      .database()
      .ref('/Leagues/' + League_ID + '/GameResults')
      .on('child_added', snapshot => {
        const collection = snapshot.val();
        if (collection) {
          var objGameResults = null;
          const TeamIDsList = [];
          var matchdate = null;
          TeamIDsList.push(collection.TeamA);
          TeamIDsList.push(collection.TeamB);
          const LeaguePromises = TeamIDsList.map(TeamID => {
            return firebase
              .database()
              .ref()
              .child('Teams')
              .orderByKey()
              .equalTo(TeamID)
              .once('value', snapshot => snapshot);
          });
          Promise.all(LeaguePromises)
            .then(Teams => {
              Teams.forEach(singleObject => {
                var TList = JSON.stringify(singleObject);
                if (TList) {
                  var Team_List = JSON.parse(TList);
                  Object.keys(Team_List).forEach(key => {
                    if (key == collection.WinnerTeam) {
                      Team1Name = Team_List[key].TeamName;
                    } else {
                      Team2Name = Team_List[key].TeamName;
                    }
                  });
                }
              });
              GameResults.push({
                Scoredate: matchdate, //matchdate,
                ScoreData: {
                  winnerTeam: {
                    label: Team1Name,
                    value: 123,
                    division: 'South Division',
                    ps: collection.TeamAScore,
                  },
                  loserTeam: {
                    label: Team2Name,
                    value: 123,
                    division: 'South Division',
                    ps: collection.TeamBScore,
                  },
                },
              });

              this.setState({
                GameResults,
                isLoading: false,
              });
              // do something with the data
            })
            .catch(err => {
              // handle error
              this.setState({ isLoading: false });
            });
        }
      });

由于我无法编写正确的查询,结果未如预期

0 个答案:

没有答案