Dears, Please advise on how can I save the output from Firestore's request (PLEASE READ THE COMMENTS IN THE CODE BELOW):
const firestore = firebase.firestore();
export const fetchedTickets = []; //I want to store the fetched tickets from a collection "tickets" and use it somewhere else
//Starting to listen changes in Tickets collection
const unsubscriberForTicketsListener = firestore
.collection("tickets")
.onSnapshot((snapshot) => {
snapshot.docChanges().forEach((change) => {
if (change.type === "added") {
// console.log("Added Ticket: ", change.doc.data());
fetchedTickets.push(change.doc.data());
}
if (change.type === "modified") {
console.log("Edited Ticket: ", change.doc.data());
}
if (change.type === "removed") {
console.log("Remvoed Ticket: ", change.doc.data());
}
});
console.log(fetchedTickets) //From here, I can reach the fetched tickets, but I can't return this array, or do something valuable with it (store, for example)
});
console.log(fetchedTickets) //Here, fetchedTickets is just an empty array, so I can't return it's value to use it in a different file, for example