首页 > 解决方案 > 如何在数组中获取我的 documentSnapshot?

问题描述

我想连接 2 个数组。

第一个应该在我的 this.state.TeamOthers

querySnapshot.forEach(async (doc) => {

  let id = doc.id;
  firestore()
    .collection("Teams")
    .doc(id)
    .collection("membersList")
    .where("id", "==", await AsyncStorage.getItem("userID"))
    .get()
    .then((querySnapshot) => {
      if (querySnapshot.empty) {
        console.log("no doc found");
      } else {
        querySnapshot.forEach(async (docMember) => {
          let valid = docMember._data;
          console.log("validé", valid);
          firestore()
            .collection("Teams")
            .doc(id)
            .collection("membersList")
            .where("statut", "==", "Validé")
            .get()
            .then((querySnapshot) => {
              if (querySnapshot.empty) {
                console.log("no validate profile");
              } else {
                querySnapshot.forEach((valide) => {
                  let TeamsMem = valide._data;

                  firestore()
                    .collection("Teams")

                    .get()
                    .then((documentSnapshot) => {
                      if (documentSnapshot.exists) {
                        let TeamsOther = documentSnapshot._data;
                        console.log(TeamsOther);

                        this.state.TeamsOthers.push(TeamsOther);
                      }
                      return;
                    });

在我的 console.log(TeamsOthers)上,我明白了:

[2021 年 5 月 18 日星期二 11:20:40.262] LOG {“活动”:“TennisLamb”,“地址”:“尼斯”,“城市”:“尼斯”,“所有者”:true,“成员”:“5” ,“文本”:“coucou toi”,“tokenTeam”:“ohxur2pqkf”,“uid”:“3SGn1EWf9hP2DqU0mExGII2B7hI3”} [2021 年 5 月 18 日星期二 11:20:40.263] LOG {“活动”:“TennisLamb2”,“地址” :“Nice”,“City”:“Nice”,“Owner”:true,“members”:“5”,“text”:“coucou toi”,“tokenTeam”:“lc8u2d88io7”,“uid”:“3SGn1EWf9hP2DqU0mExGII2B7hI3 "}

所以即使我推送我的数据,她也不想出现在一个数组中。

其次,我得到了我的 this.state.TeamsPerso :

firestore()
      .collection("Teams")
      // Filter results
      .where("uid", "==", await AsyncStorage.getItem("userID"))
      .get()
      .then((querySnapshot) => {
        if (querySnapshot.empty) {
          console.log("no documents found");
        } else {
          querySnapshot.forEach(async (doc) => {
            let TeamsPerso = doc._data;

            this.state.TeamsPerso.push(TeamsPerso);
            console.log("TeamsPerso", TeamsPerso);
            
          });
        }
      });
  };

在我的 console.log 上与以前的状态相同:

[2021 年 5 月 18 日星期二 11:20:40.800] LOG TeamsPerso {"Activity": "ping pong", "Adress": "", "City": "Nice", "Owner": true, "members": "" ,“文本”:“”,“tokenTeam”:“h9oh21iq235”,“uid”:“uqb4wvB5LIOD3xxjN8Tr7V6qmM72”}

TeamsPerso {“活动”:“Patin sur glace”,“地址”:“Plage”,“城市”:“Nice”,“所有者”:true,“成员”:“6”,“文本”:“patin sur sable ", "tokenTeam": "stikouajl", "uid": "uqb4wvB5LIOD3xxjN8Tr7V6qmM72"}

然后我想连接所有这些数据,我尝试了几种方法:

let finalTeam = this.state.TeamsPerso.concat(
              this.state.TeamsOthers
            );
  console.log("teamsOthers", finalTeam);

我的结果只是我的 TeamsPerso :

teamsOthers [{"Activity": "ping pong", "Adress": "", "City": "Nice", "Owner": true, "members": "", "text": "", "tokenTeam" :“h9oh21iq235”,“uid”:“uqb4wvB5LIOD3xxjN8Tr7V6qmM72”},{“活动”:“Patin sur glace”,“地址”:“Plage”,“城市”:“尼斯”,“所有者”:true,“成员” :“6”,“文本”:“patin sur sable”,“tokenTeam”:“stikouajl”,“uid”:“uqb4wvB5LIOD3xxjN8Tr7V6qmM72”}]

你知道我如何连接 TeamsPerso 和 TeamsOther 吗?

标签: javascriptreact-nativegoogle-cloud-firestoreconcatenation

解决方案


推荐阅读