Home > Net >  React Native - How to deal with this
React Native - How to deal with this

Time:11-30

I'm a beginner and don't know how to go about it. I have a "state" collection and a sub-collection "city". The city has a people field with the amount of people. How to download the number of people in a city and display it in the country view.

enter image description here enter image description here


const getData = () => {
      firestore().collection('users').doc(user.uid).collection('country')
        .onSnapshot(
           querySnapshot => {
            const countryData = [];
            let total = 0;
              querySnapshot.forEach(doc => {

              countryData.push({...doc.data(), id: doc.id}); 


                firestore()
                .collection('users')
                .doc(user.uid)
                .collection('country')
                .doc(doc.id)
                .collection('city')
                  .onSnapshot(
                     querySnapshot => {
                      const itemPeople = [];
                      let totalPeople = 0;
                        querySnapshot.forEach(doc2 => {
                           
                           totalPeople  = doc2.data().people;
                           itemPeople.push({totalPeople});
                                   
                        });

                      setItemPeople(totalPeople);     
                        }, 
                 )
                  
              });
             
                   setCountryData(countryData);
                
              },
                error => {
                 console.log(error)
              }
          
        )
    };
  
    useEffect(() => {
      getData();
    }, []);

CodePudding user response:

Here is the example how you can get the people value

const result = firebase
  .firestore()
  .collection("Country")
  .doc("your user's UID")
  .collection("City")
  .get()
  .then(querySnapshot => {
    querySnapshot.forEach(queryDocumentSnapshot => {
      console.log(queryDocumentSnapshot.get("People"));
    });
  })
  .catch(err => {
    alert(err);
  });

CodePudding user response:

I did something like this but it doesn't quite work as it should, because it fetches data sums up but, sometimes it shows 2 or 3 countries, and 3 are entered

               .get()
                .then(querySnapshot => {
                  let itemCity = [];
                  
                  querySnapshot.forEach(doc2 => {

                  itemCity.push(doc2.get('people'))
                  });
                 
                  
                   setTotal(itemCity);

                   let sum = 0;
                    for (let i = 0; i < itemCity.length; i  ) {
                      sum  = itemCity[i];
                    }

                   countryData.push({...doc.data(), id: doc.id, sum });

                })   
              });
              setCountryData(countryData);

CodePudding user response:

I have a problem with passing the sum values below, I don't know how to pass so where are these '???' characters As setState passes, it inserts the last calculated value, and it should insert in turn. You can help ?

.get()
  .then(querySnapshot => {
    let itemCity = [];
      querySnapshot.forEach(doc2 => {

       itemDiary.push(doc2.get("people"));
    
                    
      });

      setSum(itemCity);
                 
      let sum = 0;
      for (let i = 0; i < itemCity.length; i  ) {
          sum  = itemCity[i];
      }
      })
 
      diaryCountry.push({...doc.data(), id: doc.id, sum:???? sum???});
      setCountryData(countryData);
   
   });
  • Related