简体   繁体   English

从Firebase数据库表中检索列表中的数据

[英]Retrieving data in a list from firebase database table

I have a Profile table in my firebase database that looks like this 我的Firebase数据库中有一个Profile表,看起来像这样 在此处输入图片说明

What I am trying to do is to grab certain values from this table for each instance of profile and list them in the UI. 我想要做的是从此表中为每个配置文件实例获取某些值,并将其在UI中列出。 I found another stack overflow post and the solution that worked for them was like this... 我发现了另一个堆栈溢出帖子,为他们工作的解决方案是这样的...

here is the home.ts file (this will be where I display up all the users) 这是home.ts文件(这是我显示所有用户的位置)

import { Component } from '@angular/core';
import { NavController, ToastController, NavParams } from 'ionic-angular';
import { AngularFireAuth } from 'angularfire2/auth';
import { AngularFireDatabase, AngularFireObject } from 'angularfire2/database';
import { Profile } from '../../models/profile';
import { Observable } from 'rxjs/Observable';
import { HttpClient } from'@angular/common/http';

import firebase from 'firebase';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {

  profiles:Observable<any>;
  constructor(public http: HttpClient, private toast: ToastController, public afDatabase: AngularFireDatabase, private afAuth: AngularFireAuth, public navCtrl: NavController, public navParams: NavParams) {
    let profiles = this.afDatabase.list('profile').valueChanges();
  }
  }

And then in the home.html file, I am trying to display it like this.. 然后在home.html文件中,我试图像这样显示它。

<ion-list>
      <ion-item *ngFor="let profile of profiles | async">
        <p>{{profile.bandName}}</p>
        <p>{{profile.bandHandle}}</p>
      </ion-item>
  </ion-list>

I get 0 errors, but absolutely nothing is populating. 我收到0个错误,但绝对没有填充。 No values are showing on the UI! UI上没有显示任何值! Any ideas here as to what could be going wrong? 关于可能出问题的任何想法?

You're trying to store the value onto a block scoped variable profile . 您试图将值存储到块作用域变量profile The variable won't be accessible in the html this way. 这种方式将无法在html中访问该变量。

export class HomePage {
  public prolfies: any;

  constructor(public http: HttpClient, private toast: ToastController, public afDatabase: AngularFireDatabase, private afAuth: AngularFireAuth, public navCtrl: NavController, public navParams: NavParams) {
    afDatabase.list('profile')
      .valueChanges()
      .subscribe((data) => {
        this.profile = data;
      });
  }
}

Didn't try it, but I think you have to subscribe to your list. 没有尝试过,但是我认为您必须订阅您的列表。

Try: 尝试:

let profiles = this.afDatabase.list('profile').valueChanges()
   .subscribe(value => {
      this.profiles = value;
   })

If it didn't work do can you do this in your ngOnInit() 如果它不起作用,可以在ngOnInit()中执行此操作吗?

ngOnInit(
   console.log(this.profiles)
)

And then paste you here your result 然后将您的结果粘贴到这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM