【问题标题】:Firestore- How to get the id of a user that contains a specific value?Firestore-如何获取包含特定值的用户的 ID?
【发布时间】:2021-02-22 00:24:34
【问题描述】:

我的 firestore 结构包含一个名为 users 的集合,其中包含文档。每个用户的文档都包含名为 user-info 的集合,其中包含归档的 email

这是一张描述 Firestore 结构的图片:

如何从用户信息中的特定电子邮件(红色)中获取用户 ID(绿色)?

我在ts文件中写的函数是:(不起作用)

...
export class UsersList implements OnInit {
  ...
  constructor(private router: Router, private authservice: AuthService,public crudservice:CrudService) {}
  ....
  get_UserID_from_email(someEmail)
  {
    let user;
    this.crudservice.get_AllUsers().subscribe(data => {
      user = data.map(c => {
        console.log(c.payload.doc.data()['user-info']['email']);
        if(c.payload.doc.data()['user-info']['email'] == someEmail)
          return c.payload.doc;
      })
    });
  }
  ...
}

在 crud.service.ts 中:

...
export class CrudService {
  constructor(private authservice: AuthService, public fireservices:AngularFirestore) { }
  ...
  get_AllUsers()
  {
    return this.fireservices.collection('users').snapshotChanges();
  }
  ...
}

【问题讨论】:

  • 你需要查看c.payload.doc中文档的路径,才能找到组织它的父集合的名称。该对象应该有属性或方法,可以让你了解它的父集合/文档。

标签: angular firebase google-cloud-firestore


【解决方案1】:

这里我写了如何在firestore中获取子集合数据的函数。你能试试这个代码吗?

  parentId: any;
  getUser(email) {
    let docRef = this.fireservices.collection('users');
    const users = docRef.snapshotChanges().pipe(
      map((actions) =>
        actions.map((a) => {
          //this.parentId is your parent documentId which is in your image green color
          this.parentId = a.payload.doc.id;
          return docRef.doc(this.parentId).collection('user-info', ref => ref.where('email', '==', email)).valueChanges();
        })
      )
    );
    users.subscribe(data => {
      data[0].subscribe(child => {
        //Here you can get the child data which is in your image red color
        console.log(child[0]);
      });
    });
  }

或者您可以尝试使用此代码的第二种方法

  parentId: any = null;
  getUser(email) {
    let docRef = this.fireStore.collection('users');
    docRef.snapshotChanges().pipe(
      map((actions) =>
        actions.map((a) => {
          //this.parentId is your parent documentId which is in your image green color
          let parentId = a.payload.doc.id;
          return docRef.doc(parentId).collection('user-info', ref => ref.where('email', '==', email)).snapshotChanges().pipe(map((child) => child.map((childData) => {
            const data = childData.payload.doc.data() as any;
            const childDocId = childData.payload.doc.id;
            return { parentId, ...data };
          })));
        })
      )
    ).subscribe(data => {
      data[0].subscribe(childData => {
        //Here you can got the parent document Id
        childData[0].parentId;
      })
    });
  }

【讨论】:

  • 你需要 import import { map } from 'rxjs/operators';
  • 您好@Mana,很抱歉给您带来不便。我已经更新了整个答案。你能试试这个代码吗?
  • @Mana 感谢您的赞赏。我需要使用您的数据调试代码。有可能吗?
  • 如果没有,请参考此文档以解决您的问题它会帮助您github.com/angular/angularfire/blob/…
  • 我已经使用这个文档学习了 angular 的 firebase。
【解决方案2】:

试试这个:

 get_AllUsers(){
      return await this.fireservices.collection('users').doc('user-info')
                       .where('email', '==', 'try1@gmail.com').get()
                       .then(doc => {
                          return doc;
                       }).catch(error => {
                          console.log(error);
                       });
}

【讨论】:

  • 谢谢!我很感激!但是我在where(第二行)中收到一个错误:Property 'where' does not exist on type 'AngularFirestoreDocument<unknown>'. 你知道导致错误的原因吗? @Akash Bavale
  • @Mana 看来我使用的是完全不同的包,如果它解决了您的问题,请尝试这个包 "@google-cloud/firestore": "^1.2.0"
【解决方案3】:
    import { AngularFirestore, CollectionReference } from '@angular/fire/firestore';
    
    constructor(private firestore: AngularFirestore){}
    this.firestore
              .collection('collectionName', (collectionRef: CollectionReference) =>
                collectionRef.where('email', '==', 'email')
              )
              .snapshotChanges()
              .subscribe((res) => {
                if (res.length > 0) {
                  this.user = res.map((e) => {
                    return {
                      id: e.payload.doc.id,
                      ...(e.payload.doc.data() as any),
                    };
                  })[0];
You can get user list. And if u console the id. You got the firebase data id.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-10-08
    • 1970-01-01
    • 2021-07-06
    • 2018-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-20
    相关资源
    最近更新 更多