Displaying only the Firebase entries that were created by the currently logged-in user in Angular JS

Currently, I have a table displaying all entries from an "events" node in firebase.

However, I only want to display events created by the logged-in user. At the moment, it's showing events created by all users.

I'm thinking of using an ng-if directive after the ng-repeat in the tag, but I'm unsure what to include in it.

Here 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 is structured like this:

{
"provider": "password",
"uid": "635gt3t5-56fe-400d-b50b-1a6736f8874a",
"token":
"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJlbWFpbCI6Im1pY2hhZWwubGFyaXZpZXJlMTk3M0BnbWFpbC5jb20iLCJlbWFpbF92ZXJpZmllZCI6ZmFsc2UsImlhdCI6MTQ2OTEyNTYyOSwidiI6MCwiZCI6eyJwcm92aWRlciI6InBhc3N3b3JkIiwidWlkIjoiNmY5ZmM0NTUtNTZmZS00MDBkLWI1MGItMWE2NzM2Zjg4NzRhIn19.yIzzV7Or7tUlXi-sSWeioNx6LLoQ0U9qnW1X06rpSmA",
"password": {
"email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6d151515151515431515151515155c5f5e592d0a000c0401430e0200">[email protected]</a>",
"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": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7b030303030303550303030303034a49484f3b1c161a121755181416">[email protected]</a>",
  "exp": 1469212029,
  "iat": 1469125629,
  "sub": "635gt3t5-56fe-400d-b50b-1a6736f8874a",
  "auth_time": 1469125629,
  "firebase": {
    "identities": {
      "email": [
        "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="314949494949491f4949494949490003020571565c50585d1f525e5c">[email protected]</a>"
      ]
    }
  }
}
},
"expires": 1469212029
}

This is how my controller is set up:

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);
  }
});

Here is how the users and events are displayed in firebase:

https://i.sstatic.net/hf0vN.png

Answer №1

If you want to achieve the desired outcomes, consider implementing an angularjs filter.

Add a function named `filterByUID` in your controller:

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

This function serves as a filter that only allows events created by the current user to pass through, comparing the event's createdBy with the user's uid.

Next, modify this line in your HTML code:

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

to this:

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

This instructs angularjs to apply the specified filter from the controller to your items.

Additionally, check out this link for more information on utilizing custom filters: AngularJS : Custom filters and ng-repeat

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How do I make the YouTube Grid Gallery player show up in a pop-up window?

I have been experimenting with the following code: <head> <script type="text/javascript" src="http://swfobject.googlecode.com/svn/trunk/swfobject/swfobject.js"></script> <script type="text/javascript"> function loadVideo(playerUrl, ...

Encrypting data using SSL in javascript and python simulation

Due to the restrictions imposed by the Great Firewall of China, Google AppEngine's secure https port has been blocked. To ensure the protection of my users' information from being intercepted by ISPs and the GFW, I am planning to create a Secure ...

Experiencing Strange Issues with Jquery Image Carousel... Assistance Needed!

I recently created a jquery slideshow using a tutorial I found at this link: While the slideshow is functioning correctly for the most part, there is a strange issue that occurs right at the beginning when displaying the first image. Initially, the first ...

When sending a POST request, the req.body.someElement parameter is not defined

Encountering an issue with a post request, as req.body is returning undefined values. Despite researching on Google and Stack Overflow, the provided solutions have not resolved the problem. Although logging the name shows a value, trying to access req.body ...

How to selectively hide the menu button in Ionic framework when not within the <ion-view> element

Within the framework of Ionic, my goal is to dynamically hide the menu button based on certain conditions. However, due to other constraints, I had to separate the menu into its own controller to avoid completely re-rendering the menu and header bar every ...

Developer tools indicate React state property is set despite it always being undefined

Encountering a perplexing problem while using ReactJs along with TyperScript. In the constructor of the component, I initialize the state with a value from the provided props: constructor(props: IEditProps) { super(props); const initialState = { ...

How can we determine if the user is on a small device and utilizing flexbox with col-sm?

Is there a method to detect when the user is on a small device? I'm looking to adjust my navigation menu based on the size of the device, similar to the col-sm functionality. ...

Using React to create a fadeout effect and decrease the length of the photo

Is there a way to create a fade-out effect for each photo card and then shorten the length of the photos following the fade out? I have tried implementing a solution, but it doesn't seem to work when I click on each photo. Any suggestions or insights ...

"Using axios and async/await in VUE.JS to perform multiple asynchronous GET

Perhaps this is a somewhat basic inquiry, as I am still learning the ropes of vue.js and javascript. Please bear with me if my question is not well-articulated or if the solution is straightforward... I am facing an issue where I need to retrieve data from ...

Transform an array of object's designated key values into a string that is separated by commas

Is there a way to convert specific key values of an object into a comma-separated string? I have been able to do this with arrays, but my current challenge is that my data is an array of objects. I want to convert each 'id' value into an array of ...

Encountering the issue "error TS2339: Property 'user' is not available on type 'Object'."

issue TS2339: The property 'user' is not found on the type 'Object'. export class UserAuthentication implements OnInit { user = new BehaviorSubject<Userlogin>(null); constructor( private route: ActivatedRoute, private rou ...

Using v-model in Vue, the first option has been chosen

Is there a way to set a default value for myselect when a user visits the site for the first time? I want the first option to be selected initially, but allow the user to change their choice if they prefer another option. Can this be achieved using v-model ...

What are the steps to incorporate a Microsoft Project Dashboard into a web application?

Currently, I am working on a project that involves incorporating an MS Project Dashboard into my Angular-based web application with a Node backend API. Our goal is to seamlessly embed the MS Project dashboard within our application. However, I am unsure of ...

What could be causing the HTML5 canvas not to show up on the screen?

<!doctype html> <html> <head> <title>Canvas test</title> </head> <body> <script type="text/javascript"> c = getElementById('canvas'); ctx = c.getContext("2d")' ctx.fillRect(10,10,10,10); </s ...

Using filter to convert a string into an array and then utilizing it in ng-repeat functionality

I have a string in the format of "1200:2,1300:3,1400:2" that I need to display like this: <p>1200</p><p>2</p> <p>1300</p><p>3</p> <p>1400</p><p>2</p> I attempted to use a filter, ...

Top-notch java tool for caching and minifying dojo, jquery JavaScript, and CSS files

In our project, we have downloaded the Dojo and jQuery libraries from Google CDNs. I am currently seeking a Java tool that can both cache and minify these libraries. The caching process should take place within the ROOT project of Tomcat. While I am awar ...

Tips for adding color to the <td> element in an ejs file using nodeJS

I am looking to incorporate a color into my .ejs file. Here is the code snippet I am working with: <% resultList.forEach(function(item, index){ %> <tr> <td><%= item.function %></td> <td><%= item.value %>< ...

I am attempting to assign a default value to a TextField by retrieving data from a GetMapping call in React, however, the value is not being successfully set

I am facing an issue with setting a default value for a TextField in my code. Even though I am trying to populate it with data from a GetMapping call, the value is not being set as expected. Here is the JSON response I receive from the API call: { "id": 1 ...

I am looking to modify the appearance of specific sections of a searched word in React.js, such as making them bold or lighter. Can anyone

After coming across several similar questions, I realized none quite matched my issue. Specifically, I am attempting to style only the part of the search result that relates to the entered query. For instance, if the query is "Thi", I want the result to di ...

Why does the API in Next Js get triggered multiple times instead of just once, even when the strict mode is set to false?

React Query Issue I am currently facing an issue with React Query where the API is being triggered multiple times instead of just once when the selectedAmc value changes. I have tried setting strict mode to false in next.config.js, but that didn't so ...