livi1717 livi1717 - 1 year ago 98
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">
<th>Title</th><th>Location</th> <th>Actions</th>
<tr scope="row" ng-repeat="event in events | reverse" >
<td><button class="btn btn-primary" ng-click="events.$remove(event)"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span></button></td>


The user object looks like so:

"provider": "password",
"uid": "635gt3t5-56fe-400d-b50b-1a6736f8874a",
"password": {
"email": "",
"isTemporaryPassword": false,
"profileImageURL": ""
"auth": {
"provider": "password",
"uid": "635gt3t5-56fe-400d-b50b-1a6736f8874a",
"token": {
"email_verified": false,
"email": "",
"exp": 1469212029,
"iat": 1469125629,
"sub": "635gt3t5-56fe-400d-b50b-1a6736f8874a",
"auth_time": 1469125629,
"firebase": {
"identities": {
"email": [
"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
$ = $firebaseArray(Ref.child('events').limitToLast(10));
// display any errors

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

if (newEvent) {
// push a event to the end of the array
title: newEvent.title,
location: newEvent.location,
createdBy: user.uid,
createdAt: Firebase.ServerValue.TIMESTAMP
// display any errors


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 Source

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" >


<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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download