Inside a function, I first create an array of the current state, to use in a for loop. However when trying to get it's length, it returns 0. Logging both the array from the state and the new array shows that the array does include the right objects, and even shows the length as the proper amount.
console.log(this.state.teams);
console.log(this.state.teams.length);
let sortedTeams = this.state.teams;
console.log(sortedTeams);
console.log(sortedTeams.length);
What could be causing the length to be returned as 0, even though it's clearly not?? The array is being mapped from firestore as follows:
getTeamsAndSort() {
let teams = [];
docRef.collection('Teams').get()
.then(function(querySnapshot) {
querySnapshot.forEach(function(doc) {
let name = doc.data().name;
let seeding = doc.data().seeding;
let team = {name, seeding};
teams.push(team);
});
teams.sort((a, b) => (a.seeding > b.seeding) ? 1 : -1);
});
this.setState({teams});
};
The function where I need the length of teams[] is called immediately after I call getTeamsandSort(), in case it is an asynchronous issue.