livi1717 livi1717 - 4 months ago 16
Javascript Question

Firebase / Angular JS show only firebase entries created by logged in user

Right now I have a table that is currently showing all the entries form an "events" node in firebase.

However, I only want to show the events created by the logged in user. Right now they are showing events created by all users.

I'm guessing I might be able to use an ng-if directive after the ng-repeat in the tag, however, I am not sure what to put into it.

This is my table:

<table class="table table-striped">
<thead>
<tr>
<th>Title</th><th>Location</th> <th>Actions</th>
</tr>
</thead>
<tbody>
<tr scope="row" ng-repeat="event in events | reverse" >
<td>{{event.title}}</td>
<td>{{event.location}}</td>
<td><button class="btn btn-primary" ng-click="events.$remove(event)"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span></button></td>
</tr>

</tbody>




The user object looks like so:

{
"provider": "password",
"uid": "635gt3t5-56fe-400d-b50b-1a6736f8874a",
"token":
"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJlbWFpbCI6Im1pY2hhZWwubGFyaXZpZXJlMTk3M0BnbWFpbC5jb20iLCJlbWFpbF92ZXJpZmllZCI6ZmFsc2UsImlhdCI6MTQ2OTEyNTYyOSwidiI6MCwiZCI6eyJwcm92aWRlciI6InBhc3N3b3JkIiwidWlkIjoiNmY5ZmM0NTUtNTZmZS00MDBkLWI1MGItMWE2NzM2Zjg4NzRhIn19.yIzzV7Or7tUlXi-sSWeioNx6LLoQ0U9qnW1X06rpSmA",
"password": {
"email": "xxxxxx.xxxxxx1234@gmail.com",
"isTemporaryPassword": false,
"profileImageURL": "https://secure.gravatar.com/avatar/5f9effbf8cbea69792c595079cf25d38?d=retro"
},
"auth": {
"provider": "password",
"uid": "635gt3t5-56fe-400d-b50b-1a6736f8874a",
"token": {
"email_verified": false,
"email": "xxxxxx.xxxxxx1234@gmail.com",
"exp": 1469212029,
"iat": 1469125629,
"sub": "635gt3t5-56fe-400d-b50b-1a6736f8874a",
"auth_time": 1469125629,
"firebase": {
"identities": {
"email": [
"xxxxxx.xxxxxx1234@gmail.com"
]
}
}
}
},
"expires": 1469212029
}


My controller looks like this:

angular.module('myApp').controller('ChatCtrl', function($scope, user,
Ref, $firebaseArray, $timeout) {

console.dir('user: ' + JSON.stringify(user));

// synchronize a read-only, synchronized array of events, limit to most recent 10
$scope.events = $firebaseArray(Ref.child('events').limitToLast(10));
// display any errors
$scope.events.$loaded().catch(alert);

// provide a method for adding a event
$scope.addEvent = function(newEvent) {


if (newEvent) {
// push a event to the end of the array
$scope.events.$add({
title: newEvent.title,
location: newEvent.location,
createdBy: user.uid,
createdAt: Firebase.ServerValue.TIMESTAMP
})
// display any errors
.catch(alert);
}

};

function alert(msg) {
$scope.err = msg;
$timeout(function() {
$scope.err = null;
}, 5000);
}
});


This is what the users and events look like in firebase:

enter image description here

Answer

To get the results that you're looking for, try using an angularjs filter.

In you controller add a function called

$scope.filterByUID = function(event) {
    if (event.createdBy === user.uid) {
        return true;
    }
    return false;
}

This function will act as a filter that only let's through events that were created the current user by comparing the event's createdBy to the user's uid.

Then change this line in your html

<tr scope="row" ng-repeat="event in events | reverse" >

To

<tr scope="row" ng-repeat="event in events | reverse | filter:filterByUID" >

This tells angularjs that you want to have your items filtered with the filter we defined in the controller.

Edit: Here's a reference on using custom filters: AngularJS : Custom filters and ng-repeat

Comments