In ionic app, I want to display the data stored in firebase firestore to the app

最后都变了- 提交于 2020-01-06 07:06:06

问题


In ionic, I want to display the user's email and location which are stored in firestore, but the user's firestore document id is their email address. How can i get their data and display to them

In authentication.ts

import { Injectable } from "@angular/core";
import { AngularFirestore } from '@angular/fire/firestore';
import * as firebase from 'firebase/app';

@Injectable()
export class AuthenticationService {

  constructor(private firestore: AngularFirestore){}

  registerUser(value){

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

     this.firestore.doc(`users/${value.email}`).set({
      Email: value.email
    })
  }


  userDetails(){
    return firebase.auth().currentUser;
  }
}

In display.page.html

<ion-header>
  <ion-toolbar>
    <ion-title>
      User Welcome
    </ion-title>

  </ion-toolbar>
</ion-header>

<ion-content padding>

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

  <ion-button (click)="logout()">Log Out</ion-button>
</ion-content>

In display.page.ts, I have given code to add email and location to firestore now i want to display the data what i am doing is

display.page.ts

ngOnInit(){

    this.authService.read().subscribe(data => {

      this.info = data.map(e => {
        return {
          Email: e.payload.doc.data()['Email'],
          Location: e.payload.doc.data()['Location'],
        };
      })
      console.log(this.info);
    });
}

In authentication.ts

read() {
    return this.firestore.collection('users').snapshotChanges();
  }

The problem is it displaying all the documents data


回答1:


In your service you could use something like this to retrieve that user's data:

getUser(id: string) {
   return this.firestore.collection('users').doc(id).get();
}

Then you call this function within your component like this passing your key (email in your case):

this.someService.getUser(email).then((value) => {
    console.log(value.data)
}


来源:https://stackoverflow.com/questions/57043952/in-ionic-app-i-want-to-display-the-data-stored-in-firebase-firestore-to-the-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!