Erikson, the firebase get is being asynchronous, so you give the Return even before the business array is filled.
You can pass getEmpresas to return a Promise, then when using this method, you do return then or use await:
//Método
async getEmpresas() {
return new Promise( (resolve, reject) => {
let empresas = [];
firebase.firestore().collection('empresa').get().then(snapshot => {
snapshot.docs.forEach(empresa => {
empresas.push(empresa.data());
console.log(empresas);
});
resolve(empresas);
});
});
}
//Consumo com then
getEmpresas()
.then( empresas => {
console.log(empresas);
});
//Comsumo com wait
let empresas = await getEmpresas();
Another way would be to make the consumption of firebase synchronous with await:
async getEmpresas() {
let empresas = [];
let snapshot = await firebase.firestore().collection('empresa').get();
snapshot.docs.forEach(empresa => {
empresas.push(empresa.data())
console.log(empresas)
});
return empresas;
}
There are many other ways, gives a researched on async/await, helped me a lot for cases similar to yours:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/funcoes_assincronas