【问题标题】:In ionic app, I want to display the data stored in firebase firestore to the app在 ionic 应用程序中,我想将存储在 firebase firestore 中的数据显示到应用程序
【发布时间】:2023-04-04 01:35:01
【问题描述】:

在 ionic 中,我想显示存储在 firestore 中的用户电子邮件和位置,但用户的 firestore 文档 ID 是他们的电子邮件地址。我如何获取他们的数据并显示给他们

在 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;
  }
}

在 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>

在 display.page.ts 中,我已经给出了将电子邮件和位置添加到 firestore 的代码,现在我想显示我正在做的数据

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);
    });
}

在 authentication.ts 中

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

问题是显示所有文档数据

【问题讨论】:

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


    【解决方案1】:

    在您的服务中,您可以使用类似的方法来检索该用户的数据:

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

    然后你在你的组件中调用这个函数,就像这样传递你的密钥(在你的情况下是电子邮件):

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

    【讨论】:

    • 错误:未捕获(在承诺中):FirebaseError:[code=invalid-argument]:函数 CollectionReference.doc() 要求其第一个参数为非空字符串类型,但它是:未定义FirebaseError:函数CollectionReference.doc()要求其第一个参数为非空字符串类型,但它是:未定义此错误弹出
    • 您没有将变量正确传递给函数,请检查 ID 是否作为参数正确传递。您的服务函数将该变量设为未定义。
    • 请告诉我该怎么做
    • 在您的 ngOnInit 方法中,您需要从服务中调用 getUser 并将电子邮件作为参数传递给函数。您是否将该电子邮件保存在您的服务或本地存储中的某个位置?你需要那个变量
    猜你喜欢
    • 1970-01-01
    • 2019-11-08
    • 1970-01-01
    • 2016-05-28
    • 1970-01-01
    • 1970-01-01
    • 2021-10-23
    • 2018-08-06
    • 1970-01-01
    相关资源
    最近更新 更多