Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6287

how to use on() firebase database method to return value to the calling function?

$
0
0

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


Viewing all articles
Browse latest Browse all 6287

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>