Soy duro con este problema, en realidad obtuve objetos de firebase y los empujé en una matriz, pero cuando introduzco esto en foreach me da un resultado vacío:
Consola:
[] 0: {org_Address: 'Talisay City', middle_Name: 'Galve', rescuer_Num: 45344, last_Name: 'Primavera', first_Name: 'Jazon', …} 1: {coordinates: Kc, org_Address: 'Talisay City', rescuer_Num: 54552342, first_Name: 'Francisco', middle_Name: 'Palinguva', …} 2: {rescuer_Num: 533452, rescue_Org: 'Talisay City Disaster Risk Reduction and Management Office (TCDRRMO)', coordinates: Kc, date_Registered: at, first_Name: 'Berto', …} 3: {middle_Name: 'Arroyo', org_Address: 'Talisay City', rescuer_Num: 545234212, last_Name: 'Donasco', first_Name: 'Brent', …} 4: {coordinates: Kc, middle_Name: 'Delacruz', date_Registered: at, first_Name: 'John', rescuer_Num: 2342314, …} 5: {coordinates: Kc, first_Name: 'Kenneth John', middle_Name: 'N/A', rescuer_Num: 534534535, org_Address: 'Talisay City', …} 6: {last_name: 'adsf', rescue_Org: 'asdf', middle_name: 'adsf', first_name: 'asdf', org_Address: 'asdf', …} 7: {middle_Name: 'Philip', rescue_Org: 'Radiant Volunteer', first_Name: 'Ace', coordinates: Kc, rescuer_Num: 123124, …} 8: {last_name: 'aaa', rescuer_Num: 'aaa', org_Address: 'aaa', rescue_Org: 'aaa', middle_name: 'aaa', …} 9: {org_Address: 'Talisay City', coordinates: Kc, rescuer_Num: 4353453, rescue_Org: 'Talisay City Disaster Risk Reduction and Management Office (TCDRRMO)', last_Name: 'Cock', …} length: 10 [[Prototype]]: Array(0)Esta es mi implementación:
// from other source file export async function firebaseReadDoc(targetDocument) { let dataCollected = []; getDocs(collectionReference(targetDocument)) .then(snapshot => { snapshot.docs.forEach(doc => { dataCollected.push({...doc.data(), id: doc.id }); }); console.log(SUCCESS_READ_FIREBASE); }).catch(e => { console.error(WARNING_FAIL_READ_FIREBASE); console.error(e.message); }); return dataCollected } // from other source file function readDocRescuer() { const targetDocument = "public_rescuers"; const data = firebaseReadDoc(targetDocument); data.then(response => { console.log(response); response.forEach(doc => { console.log(doc.first_name); }) }) }luego, cuando registro doc.first_name, no se muestra.
Como explica Tu Nguyen, debe administrar el aspecto asíncrono del método getDocs() . Lo siguiente debería hacer el truco:
// from other source file export async function firebaseReadDoc(targetDocument) { let dataCollected = []; const snapshot = getDocs(collectionReference(targetDocument)); snapshot.docs.forEach(doc => { dataCollected.push({...doc.data(), id: doc.id }); }); return dataCollected } // from other source file async function readDocRescuer() { // async function const targetDocument = "public_rescuers"; const dataArray = await firebaseReadDoc(targetDocument); dataArray.forEach(doc => { console.log(doc.first_name); }) }O con una función no asíncrona.
function readDocRescuer() { // non async function const targetDocument = "public_rescuers"; firebaseReadDoc(targetDocument) .then(dataArray => { console.log(dataArray); dataArray.forEach(doc => { console.log(doc.first_name); }) }) }firebaseReadDoc es una función asíncrona (devuelve una Promesa), por lo que tendrá que await firebaseReadDoc(targetDocument) en readDocRescuer . También deberá hacer que readDocRescuer sea una función asíncrona.
Alternativamente, puede hacer firebaseReadDoc(targetDocument).then(//read your data);