Advertisement
Guest User

Untitled

a guest
Jul 28th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. function eventusers(id) {
  2. var userarr = [];
  3. var deferred = $q.defer();
  4.  
  5. // *The code below makes 2 firebase calls and returns an array of users*
  6.  
  7. eventref.orderByChild("Eventid").equalTo(eventid).on("value", function(snap) {
  8. var users = snap.val();
  9. angular.forEach(users, function(value,key) {
  10. var obj = value;
  11. for (var prop in obj) {
  12. if(obj[prop] == "True") {
  13. userref.child(prop).on("value", function (snap) {
  14. var id = snap.val().email;
  15. userarr.push(id);
  16. console.log(userarr); // I am able to see the list of users here
  17. });
  18. };
  19. };
  20.  
  21. });
  22. deferred.resolve(userarr);
  23. });
  24. return deferred.promise;
  25. };
  26.  
  27. //The console.log shows a promise (pls see the attached pic)
  28. console.log(eventusers(eventid));
  29.  
  30. // I tried to loop through the response using angular.forEach and also a for loop but it does
  31. not execute that part of the code as I do not see the response of the console.log. If I
  32. replace the for loop with just console.log(response), then I get an empty array.
  33.  
  34. eventusers(eventid).then(function (response) {
  35. for (var i = 0; i <response.length; i++) {
  36. console.log(response[i]);
  37. };
  38. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement