Retrieve the current user's posts using Angular and Firebase

I'm currently working on an Angular-Firebase Ionic app and running into a problem when trying to retrieve the current user's posts from Firebase. My main goal is to fetch the current user's posts.

Below is the code snippet:

postss: any;
constructor(

private afAuth: AngularFireAuth,
) {
this.postss = this.afDB.list('users/posts').valueChanges();

this.afAuth.authState.subscribe(user => {

      }

Here's the corresponding HTML:

<ion-list no-lines>
<ion-card  *ngFor="let post of posts ">
<li><ul>{{post}} </ul></li>

Shown below is a screenshot:

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

Answer №1

Instead of using

this.postss = this.afDB.list('users/posts').valueChanges();
, try
this.postss = this.afDB.list('users/posts/' + user.uid).valueChanges();
. It's important to make sure you retrieve the currentUser.uid before setting up your this.afDB.list statement.

Answer №2

When it comes to accessing objects in a database, the use of 'list' on its own will retrieve all items within that path.

To be more specific and target certain objects, you can implement a query like this:

this.fireDB.list('users/posts', ref => ref.orderByChild('user_id').equalTo('123')).snapshotChanges().subscribe(res => {

    res.forEach(doc => {
        this.posts.push(doc.payload.val());
        console.log(doc.payload.val()); // This will display the values of each post.
        console.log(doc.key); // This will reveal the ID of the object entry in the database.
    });

});

For instance, the above query retrieves objects from users/posts in the database where user_id is equal to 123.

For further information on angularfire2, check out this link.

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

Exploring Angular's ngFor feature in building a dynamic accordion menu that automatically closes one panel and opens another when clicked

The Angular Material accordion component is a key feature in my Angular project. Utilizing an ngFor loop to iterate through the information stored in menuItems, I dynamically create a new expansion panel for each item. With two components in play, I seaml ...

Angular 4 and Webpack: Compilation Error

After successfully running npm install, I encountered an error when trying to execute ng serve. Despite multiple attempts and troubleshooting, the issue persists. Could this be related to Angular versions? Interestingly, the same project runs smoothly on ...

We encountered a surprising character "EOF" in the code. Could it be due to an unescaped "{" in the template? Please use "{{ '{' }}" to properly escape it

I encountered an issue while upgrading my angular 2 app from beta 9 to RC5. The error I received is in my form template. Below is the detailed error: zone.js:461 Unhandled Promise rejection: Template parse errors: Unexpected character "EOF" (D ...

TypeScript is unaware that a component receives a necessary prop from a Higher Order Component (HOC)

My component export is wrapped with a higher-order component (HOC) that adds a required prop to it, but TypeScript seems unaware that this prop has already been added by the HOC. Here's the Component: import * as React from "react"; import { withTex ...

When transitioning between views in Angular App, it freezes due to the large data response from an HTTP request

I am encountering an issue with my Angular 9.1.11 application where it freezes after navigating from one page to another (which belongs to a different module with lazy loading). Here is the scenario: There is an action button called View Report that re ...

The Next.js 14 App Router is encountering a 405 Method Not Allowed error when attempting a POST request in the API route. Interestingly, the same route structure

In my Next.js 14 application using the App Router, I have encountered a peculiar issue with two API routes: While both routes function properly when running locally, only one of them works after deploying to Vercel. The "/api/vision/describe-image/route.t ...

The functionality of using `import * as firebase from 'firebase'` has been discontinued since the Angular 6 update

After upgrading my Angular 5 project to Angular 6, I've been facing challenges with building and deploying all day. The latest issue I encountered is related to importing - something that was functioning properly before. The current error message sta ...

Tips for utilizing ng2-dragula and resizing Angular elements efficiently

Currently, I am employing Dragula for drag-and-drop functionality in my tables. Additionally, I am utilizing the angular resizer element plugin for table column resizing. These features are being implemented within Angular2. However, I am facing an issue ...

The best location for storing Typescript files within an ASP.NET Core project

My Typescript app is built on AngularJS 2 with ASP.NET Core, and currently I store my TS files in the wwwroot directory. While this setup works well during development, I am concerned about how it will function in production. I aim to deploy only minified ...

Enhance your Next.js routing by appending to a slug/url using the <Link> component

In my Next.js project, I have organized my files in a folder-based structure like this: /dashboard/[appid]/users/[userid]/user_info.tsx When using href={"user_info"} with the built-in Next.js component on a user page, I expect the URL to dynamic ...

Element not recognized: <my-company-form-extra> - have you properly registered this component?

I've been attempting to render a component using the is directive <template> <div> <v-tabs v-model="currentTab" fixed-tabs> <v-tab v-for="(item, i) in tabItems" :key="i">{{ item }} < ...

Exploring the topic of nested Firestore listeners and effectively managing the process of unsubscribing

I am interested in implementing a nested listener structure similar to the following: snapshotListeners() { firestore() .collectionGroup() .where() .onSnapshot({ error: //Handle error next: firstSnapshot => { first ...

The constant LOCALE_ID is always set to en-US and remains unchanged

app.module.ts import { registerLocaleData } from '@angular/common'; import localeIndia from '@angular/common/locales/en-IN'; import additionalLocaleIndia from '@angular/common/locales/extra/en-IN'; registerLocaleData(localeInd ...

TypeScript's function types

Regarding my previous inquiry: Transforming PropTypes compatibility into TypeScript If I have this specific function to pass: const displayIcon = () => <span class='material-icons'>account_circle</span> And, the displayIcon func ...

What is the functionality of input onChange in React when using state management?

Whenever I try to log the value of the input after each onChange event, there seems to be an odd one event delay. For example, if 'some text' is the default input value and I remove the letter 't' by pressing backspace/delete, it first ...

Using checkboxes to filter a list within a ReactiveForm can result in a rendering issue

I have implemented a dynamic form that contains both regular input fields and checkboxes organized in a list. There is also an input field provided to filter the checkbox list. Surprisingly, I found out that when using the dot (.) character in the search f ...

Obtaining the specified cell in a row when a button is clicked

I'm currently grappling with how to retrieve the value of the nth cell in the same row as the button that was clicked in angular2. I understand that I need to pass the $event value, but I'm unsure of how to extract the relevant data. In app.comp ...

No routes found: Unable to match base href suffix

My Angular application is deployed on an IIS web server at my-machine:8111/my-app. I have configured both the base tag and the APP_BASE_HREF value according to the guidance provided in this answer, setting them to the absolute URL for the app: https://my-m ...

Exploring Angular: Retrieving a Variable in TypeScript Using a String Passed from HTML

Is there a way to retrieve the value of a variable in .TS by passing its name as a string from HTML? For example: In HTML: <div id="myID"><button mat-button (click)="foo('name')">Export to Excel</button></div> In TS v ...

Upgrading from Angular 6 to Angular 7

After upgrading my Angular 4 app to Angular 6, I'm now looking to make the jump to Angular 7. According to a recent article, running the command below should only take around 10 minutes for the upgrade process: ng update @angular/cli @angular/core F ...