0

I feel like I'm so close, but I can't figure this out.

I have a data tree in Firebase with events and under each event is a list of participants with their user id and name.

-events
    -UniqueID
        -participants
            -userID
                facebook_id: "12345678"
                display_name: "John Doe"
            -userID
                facebook_id: "12345678"
                display_name: "John Doe"
            -userID
                facebook_id: "12345678"
                display_name: "John Doe"

In my routes I have a resolve function that queries the Facebook API for users who have my app installed and passes a friends object into the controller.

This is a page for the user to invite friends to the event. I want to check the participants array and remove users who are already in the event from the friends object.

I loop through the $firebaseArray and use lodash to find the corresponding id. Lodash's _.find returns the object if found or undefined if not found.

I found this removeItemsById function in another answer here, but it doesn't seem to work.

The _.find works correctly because I see it log the matched item, but it doesn't get removed from the object. Any ideas?

    $scope.participants = $firebaseArray(firebase.database().ref().child("events").child($stateParams.eventID).child("participants"));    

    $scope.participants.$loaded().then(function(){

        console.log(friends);

          angular.forEach($scope.participants, function(part){

            var i = _.toString(part.facebook_id);
            console.log(part.facebook_id);
            var f = _.find(friends, {'id': i});

            console.log("f value is: " + f);

            if (f) {
                console.log("Found matching facebook id: " + part.facebook_id);
                removeItemsById(friends, part.facebook_id);
            }

          })  

          $scope.friends = friends;

    })



    function removeItemsById(arr, id) {
        var i = arr.length;
        if (i) {   // (not 0)
            while (--i) {
                var cur = arr[i];
                if (cur.id == id) {
                    arr.splice(i, 1);
                }
            }
        }
    }
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Removing items from an existing `$firebaseArray` sounds like a problem. Instead either [exclude them with a query](https://firebase.google.com/docs/database/web/lists-of-data#filtering_data) if possible or [extend `$firebaseArray`](http://stackoverflow.com/questions/30299972/joining-data-between-paths-based-on-id-using-angularfire) to do the client-side filtering otherwise. – Frank van Puffelen Apr 13 '17 at 05:11
  • Thanks for the response. I'm not removing them from the firebase array. I'm trying to check the object returned from my Facebook api call (friends object) and remove entries from it that are also in the participants $firebaseArray. – Cory Wolff Apr 13 '17 at 05:15

0 Answers0