@IonC wrote:
I’m working on an Ionic 4 Project, where I want to display all the posts from the current user once the user log into the application. Currently, I’m using
snapshotChanges()andsubscribe()method (as shown below) to get the data from firestore. But it gives me every single data from the firestore document. Having theuserIDandpostIDreferences on both document, how can I get the value ofauthoranddescfrom that specific user or is it possible? To be specific, how can I display user(6YbhQux2sgTH66F0cJpdcT87Pw83)'s post(54d039ec-5b3c-4ee9-95a0-418b06365f25)that containsauthoranddesc?Firestore:
Here’s what I did:
getData.service.ts
import { Injectable } from '@angular/core'; import { AngularFirestore } from '@angular/fire/firestore'; @Injectable({ providedIn: 'root' }) export class getData { constructor( private firestore: AngularFirestore ) { } readData() { return this.firestore.collection('promotions').snapshotChanges(); } }post.ts
import { AngularFirestore } from '@angular/fire/firestore'; @Component({ selector: 'app-post', templateUrl: './post.page.html', styleUrls: ['./post.page.scss'], }) export class PostPage implements OnInit { constructor( private afs: AngularFirestore ){} ngOnInit() { this.getData.readData().subscribe(data => { this.posts = data.map(e => { return { id: e.payload.doc.id, Author: e.payload.doc.data()['author'], Description: e.payload.doc.data()['desc'], }; }) console.log(this.posts); }); } }post.html
<ion-card no-padding *ngFor="let post of posts"> <ion-card-header> <ion-card-title>{{post.Author}}</ion-card-title> </ion-card-header> <ion-card-content> <p>{{ post.Description }}</p> </ion-card-content> </ion-card>I also tried using
afAuth.auth.currentUserto get the current user id and the posts but it shows an errorProperty 'map' does not exist on type 'DocumentSnapshot'. I am new to this and not sure how to proceed.let user = this.afAuth.auth.currentUser; uid = user.uid; this.afs.firestore.collection('users').doc(uid).get() .then(doc=>{ var a = doc.data().posts console.log(a); this.afs.firestore.collection('posts').doc() .get().then(doc2=>{ this.promotions = doc2.map(e=>{ return { id: e.payload.doc2.id, Author: e.payload.doc2.data()['author'], Description: e.payload.doc2.data()['desc'], }; }) }) })Can anyone help? Thanks.
Posts: 1
Participants: 1
