【问题标题】:@angular/fire Realtime Database + Ionic How to print console.log list observable@angular/fire 实时数据库 + Ionic 如何打印可观察到的 console.log 列表
【发布时间】:2019-03-18 23:24:16
【问题描述】:

背景

我正在使用 ionic 4,“@angular/fire”:“^5.0.2”,“rxjs”:“^6.3.3”,“rxjs-compat”:“^6.3.3”。

问题

如何打印 console.log 可从 Firebase 观察到的列表?包括键和值。我期待一个 JSON 格式的数组。

我可以读取数据库,但是在 console.log 中看不到它。我实际上想使用从可观察的 Firebase 列表中检索到的值用于另一个功能,但是因为我看不到 console.log 中的值,所以我不知道它的行为。

代码

这是我想要从中获取值的 Firebase 节点

这是我的home.ts

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, ToastController } from 'ionic-angular';

import { AngularFireAuth } from '@angular/fire/auth';
import { AngularFireDatabase, AngularFireList } from '@angular/fire/database';
import { UserProfile } from './../../models/user-profile';
import { RestoProfile } from './../../models/resto-profile';

import { OrderMenuPage } from '../order-menu/order-menu';


@IonicPage()
@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {
  restoProfileData: AngularFireList<RestoProfile[]>;
  restoProfileRef: AngularFireList<RestoProfile[]>;

  constructor(private afAuth: AngularFireAuth, private afDatabase: AngularFireDatabase,
    private toast: ToastController,
    public navCtrl: NavController, public navParams: NavParams) {
  }

  ionViewDidLoad() {
    this.afAuth.authState.subscribe(data => {
      if(data && data.email && data.uid){
        this.toast.create({
          message: `Welcome to brianApp-customer, ${data.email}`,
          duration: 3000
        }).present();

        this.restoProfileRef = this.afDatabase.list<RestoProfile>(`profile/`);
        this.restoProfileData = this.restoProfileRef.snapshotChanges();

      }
      else {
        this.toast.create({
          message: `Could not find authentication details`,
          duration: 3000
        }).present();
      }
    });
  }

这是我的home.html

<ion-header>
  <ion-navbar>
    <button ion-button menuToggle>
      <ion-icon name="menu"></ion-icon>
    </button>
    <ion-title>
      Ionic Blank
    </ion-title>
  </ion-navbar>
</ion-header>

<ion-content padding>
  Choose a restaurant?
<ion-list>
  <ion-item *ngFor="let data of restoProfileData | async" (click)="selectResto(data)">
    <h2>Key: {{ data.payload.key }}</h2>
    <h2>Location: {{data.payload.val().location}}</h2>
    <h2>ownerName: {{data.payload.val().ownerName}}</h2>
    <h2>restoName: {{data.payload.val().restoName}}</h2>
  </ion-item>
</ion-list>
</ion-content>

我尝试了console.log(this.restoProfileData);,但控制台返回了我

任何帮助将不胜感激。

【问题讨论】:

    标签: angular firebase ionic-framework firebase-realtime-database angularfire2


    【解决方案1】:

    snapshotchanges() 将返回

    Observable<AngularFireAction<DataSnapshot>[]>
    

    用于收藏。为了订阅和显示里面的内容,您可以:

     this.restoProfileData = this.restoProfileRef
        .snapshotChanges()
        .map(changes => {
          return changes.map(change => ({key: change.payload.key, ...change.payload.val()}));
        })
    

    【讨论】:

    • 感谢您的回复。这行代码放在哪里?我把它放在这里:this.restoProfileData = this.restoProfileRef.snapshotChanges().map(actions =&gt; actions.map(a =&gt; ({ key: a.key, ...a.payload.val() })) ) 但它会引发错误:this.restoProfileRef.snapshotChanges(...).map is not a function
    • 你需要使用 pipe()
    • 嗨@SureshKumarAriya你能举个更详细的例子吗,如何使用管道()
    • @Joe Wu。根据您的最新编辑,我得到了这个:this.restoProfileRef.snapshotChanges(...).map is not a function
    • 您可以使用 pipe() 函数尝试 Suresh 的方法。我猜你的组件中没有正确导入地图。
    【解决方案2】:

    您可以在 rxjs 中对 console.log() 使用 'tap' 运算符。

    this.restoProfileData = this.restoProfileRef.snapshotChanges().pipe(
       tap((data)=> console.log(data))
    );
    

    this.restoProfileData = this.restoProfileRef
        .snapshotChanges()
        .pipe(
          map(changes => ({
          changes.map(change => ({key: change.payload.key, ...change.payload.val()}));
          })),
          tap((data)=> console.log(data))
        )
    

    【讨论】:

    • 第一个选项。你需要import { tap } from 'rxjs/operators';。但是我仍然在 console.log 中得到一个抽象的元数据值。我只想记录我在 firebase 中看到的值。
    • 尝试选项 2 并告诉我
    • 第二个选项,需要在代码末尾加上);。但是这次我又遇到了一个错误:Runtime Error map is not defined
    • 您导入地图运算符是否添加了')'。立即尝试
    • 对不起。现在我添加了import { map } from 'rxjs/operators'; 但是现在浏览器和控制台什么都没有显示......
    猜你喜欢
    • 1970-01-01
    • 2019-12-16
    • 1970-01-01
    • 2019-10-26
    • 1970-01-01
    • 2019-05-21
    • 1970-01-01
    • 2019-06-04
    • 2013-04-28
    相关资源
    最近更新 更多