I am writing a firebase database helper file having functions which must return data from database to the calling function. The functions should also return the updated data if there are any changes made in the database. So the on() method is used to retrieve data.
on() method is asynchronous and and does not wait for the callback to get the data from database. React hooks could not be used here because the rule of hooks is violated as it cannot be used in called function.
What is the best way to write a helper file having functions which read and listen to database changes?
helper.ts
// called functionexport const getAssignmentList = function (uid:string) { var userRef = db.ref("UserAssignments/" + uid); userRef.on('value', snapshot => { var assignmentList = snapshot.val(); return assignmentList; });};
HomeScreen.ts
// calling functionvar assignmentList = subscriptions.getAssignment(newId);console.log('in homescreen calling function: ', assignmentList); // a list of user assignments from database should be displayed
Terminal:
in homescreen calling function: undefined