I am using a function to query firebase realtime database and get required information. The information is stored in an array which is returned.
The challenge is that function returns the array even before database fetches the record and so I get empty result. As I need to use .on to fetch multiple records, I cannot use .then with the query.
How can I ensure that va_addresses is returned after query is fully executed.
const getAddressLabel = (vp_userid) => {
let vo_obj;
let va_addresses = [];
firebase
.database()
.ref("address/" + vp_userid)
.orderByKey()
.on("child_added", function (snapAddress) {
vo_obj = {
label: snapAddress.val().addresslabel,
value: snapAddress.val().addresslabel,
};
va_addresses.push(vo_obj);
});
return va_addresses;
};