I am facing an issue in my app where I need to update an array. The process involves fetching data from the database and adding it to the array, which works fine. However, when I try to use this updated array in another method, it seems that the array is not being updated.
Interestingly, when I inspect the exerciseList
array in the DOM, I can see the data. But when I check the length of the array inside the getExercises
function, it still shows 0. It appears as though the method is being executed before the data is actually added to the array.
Any thoughts on why this might be happening?
data: () => ({
exerciseList: []
});
created() {
this.getDataBaseCollection("Exercise", this.exerciseList); // Array gets populated here
}
mounted() {
this.getExercises();
},
methods: {
getDataBaseCollection: function (CollectionName, list) {
db.collection(CollectionName).onSnapshot(snapshot => {
snapshot.forEach(doc => {
list.push(doc.data());
});
});
},
getExercises: function () {
console.log(this.exerciseList.length); // Length is still 0 even though the array should have data
}
},