如何从Firestore获取当前用户数据?

时间:2019-10-25 08:56:11

标签: typescript firebase ionic-framework google-cloud-firestore angularfire2

我正在研究Ionic 4项目,我想在该项目中显示用户登录应用程序后当前用户的所有帖子。目前,我正在使用snapshotChanges()subscribe()方法(如下所示)从firestore中获取数据。但是它为我提供了来自Firestore文档的每个数据。在两个文档上都有userIDpostID引用时,如何从该特定用户那里获取authordesc的值,或者有可能吗? 具体来说,如何显示用户(6YbhQux2sgTH66F0cJpdcT87Pw83)的帖子(54d039ec-5b3c-4ee9-95a0-418b06365f25),其中包含authordesc

Firestore:firestore image

这就是我所做的:

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>

我还尝试使用afAuth.auth.currentUser获取当前用户ID和帖子,但是显示错误Property 'map' does not exist on type 'DocumentSnapshot'。我对此并不陌生,不确定如何进行。

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'],
           };
         })
     })
  })

更新

let user = this.afAuth.auth.currentUser;
uid = user.uid;

   const docs = this.afs.collection('users');
     const userinfo = docs.snapshotChanges()
     .pipe( 
       map(actions => 
         actions.map(a => 
           { const data = 
             a.payload.doc.data(); 
             const id = a.payload.doc.id; 
             const Author = a.payload.doc.data()['author']
             return { id, data, Author
           }; 
        })
       ) 
     );
     console.log(userinfo);

输出:userInfo

2 个答案:

答案 0 :(得分:0)

要获取authordesc,请尝试以下操作:

let user = this.afAuth.auth.currentUser;
uid = user.uid;

const docs = this.afs.collection('users');
    const userInfo = docs.snapshotChanges().pipe(
      map(data => {
        return {
          id: data.payload.doc.id,
          Author: data.payload.doc.data()['author']
          Description: data.payload.doc.data()['desc']
        };
      }));

似乎您正在使用angularfire,首先将引用放在文档userid上,然后使用snapshotChanges,它返回一个Observable,然后可以检索数据。 PipeObservable类中的一种实例方法,用于将功能运算符缝合在一起(map是这些运算符之一)。

答案 1 :(得分:0)

要获取用户创建的帖子的“作者”和“描述”,您可以执行以下操作:

let user = this.afAuth.auth.currentUser;
uid = user.uid;


this.afs.firestore.collection('posts').where('user', '==', uid).get().then(posts => {
  postsCreatedByUser = posts.docs.map(e => {
    return {
      Author: e.data()['author'],
      Description: e.data()['desc'],
    };
  }))
})

请注意,为了过滤帖子,我使用.where()方法