I have created a database in firebase and populated it by the following:
database.ref("expenses").push({
desctiption: "item no 1",
note: "note 1",
amount: 20,
createdOn: 1220,
});
database.ref("expenses").push({
desctiption: "item no 2",
note: "note 2",
amount: 10,
createdOn: 1120,
});
database.ref("expenses").push({
desctiption: "item no 3",
note: "note 3",
amount: 50,
createdOn: 920,
});
Now i want to fetch it. So i would be using the following code:
database.ref("expenses").on("value", (dataSnapshot) => {
const expensesArr = [];
dataSnapshot.forEach((expense) =>
expensesArr.push({
id: expense.key,
...expense.val()
})
);
console.log("expensesArr", expensesArr);
});
I have observed that, just like in the code above, if i don't put curly braces around the forEach's callback function, only the first item gets pushed in the expensesArr.
Whereas only if i put curly braces around forEach's callback function,i.e
dataSnapshot.forEach((expense) => {
expensesArr.push({ id: expense.key, ...expense.val() })
});
the whole 3 item gets pushed into tthe expensesArr.
Can i get an explanation for that??