I am trying to populate a constant variable with a foreach loop, which it does correctly inside the loop, but it doesn't keep its value when it gets out of the loop. Does anyone know why this is happening?
I am fetching data from a firestorm database which I am then trying to save in a constant object `
const customers = {};
customersRef.get()
.then((snapshot) => {
snapshot.docs.forEach((doc) => {
customers[doc.id] = doc.data();
});
console.log(customers);
}).catch((e) => {
console.log(e.message);
});
console.log(customers);
`
This gives me the output: `
{
"correctId": {
"some": "secretData"
},
"correctId": {
"some": "secretData"
}
}
{}
`
The console.log()
within the loop gives me the correct data while the same console.log()
beneath gives me an empty object