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

How to get current user data from firestore?

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

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 和 posts 但它显示错误 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

要获得 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, first put the reference at the document userid then using snapshotChanges, which returns an Observable, you can then retrieve the data. PipeObservable class 中的一个实例方法,用于将函数运算符拼接成一个链(map 是一个这些运算符)。

为了获取用户创建的帖子的 "author" 和 "desc",您可以执行以下操作:

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() 方法