在ionic中,我想获取Firestore文档ID

时间:2019-07-16 13:27:18

标签: angular firebase ionic-framework google-cloud-firestore ionic4

在ionic中,我想要获取Firestore文档ID以显示登录用户的存储信息。我将文档名称设置为用户的电子邮件,例如 Collection [name] =用户 文档 [名称] = user@zzzz.zz

这是我在 auth.ts

中设置文档名称的方法
import * as firebase from 'firebase/app';
import { AngularFirestore } from '@angular/fire/firestore';

constructor(private firestore: AngularFirestore) {}

registerUser(value){

    firebase.auth().createUserWithEmailAndPassword(value.email, value.password)

    this.firestore.doc(`users/${value.email}`).set({
      email: value.email,
    })
    this.router.navigate(['home']);
   }

在home.ts

export class HomePage {

  usersCollection: AngularFirestoreCollection<any>
  users: Observable<any[]>;

ngOnInit() {
     this.usersCollection.doc(How do I get the user's email which is stored as 
     document name here)
}

在home.html

<div *ngFor="let item of info">
<h1>{{item.Name}}</h1>
</div>

我尝试了这个,但是当然没有用

this.usersCollection.doc(`${this.auth.user}`).ref.get()

如何获取和添加用户的文档名称,即用户登录的电子邮件地址

2 个答案:

答案 0 :(得分:0)

获取当前用户的Uid:

import { AngularFireAuth } from '@angular/fire/auth'

  constructor(private afAuth: AngularFireAuth){

           let uid = this.afAuth.auth.currentUser.uid
}

答案 1 :(得分:0)

导入:

import * as firebase from 'firebase/app';

要获取当前用户:

 var result=  firebase.auth().currentUser; //add .uid if you want

Current user Info

要获取当前用户的登录信息,您可以将其电子邮件存储在存储中,并在调用服务后像这样:

getUserById(id) {
  return new Promise((resolve, reject) => {
   this.usersCollection.doc<User>(id).valueChanges().subscribe((value:User)=> {
     value.id=id;
      resolve(value)
    });
  });
}

id是您存储空间中的电子邮件。