【问题标题】:How to get Object from database using it's Key?如何使用它的键从数据库中获取对象?
【发布时间】:2019-09-13 14:02:48
【问题描述】:

我想使用它的键从数据库中获取一个对象,但它不起作用。

这是我的数据库:

DATABASE

数据库:

...
 - toc-list
           |___ id1 (generated by firebase)
                  |___ field1
                  |___ field2

           |___ id2 (generated by firebase)
                  |___ field1
                  |___ field2

           |___ id3 (generated by firebase)
                  |___ field1
                  |___ field2

我正在尝试使用 id1 "-Ld6AAdHn6JBYcrh29Xz" 从 toc 列表中获取一个对象

我尝试过这样,但它不起作用:

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

import firebase from 'firebase';
import { AngularFireDatabase, AngularFireList } from 'angularfire2/database';

@IonicPage()
@Component({
  selector: 'page-confrontation-1',
  templateUrl: 'confrontation-1.html',
})
export class Confrontation_1Page {

  toc_id : string;
  public toc: Array<any> = [];
  public tocRef: firebase.database.Reference = firebase.database().ref('/toc-list');

  constructor(public navCtrl: NavController, public navParams: NavParams, public afDB: AngularFireDatabase) {
    this.toc_id = navParams.get('tocId');
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad Confrontation_1Page');

    // I want to show the details of the element that has the id : toc_id
    console.log(this.afDB.object('/toc-list/' + this.toc_id));

  }


}

【问题讨论】:

  • Angular Fire 使用Observables。试试this.afDB.object('/toc-list/' + this.toc_id).valueChanges() 看看这个:github.com/angular/angularfire2/blob/master/docs/rtdb/…
  • @DaniR 它返回了一个 Observable 对象,但它不是我要找的对象,它返回了这个: Observable {_isScalar: false, source: Observable, operator: MapOperator} operator: MapOperator {project: ƒ, thisArg: undefined} source: Observable {_isScalar: false, source: Observable, operator: RefCountOperator} _isScalar: false proto: Object
  • 你能提供更多代码来看看你对结果做了什么吗?
  • @DaniR 是的,我编辑了帖子
  • @DaniR 我也更新了数据库的图片,我想显示字段:度、详细信息、高度...等

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


【解决方案1】:

Observable 是异步的,你必须订阅它。试试:

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

import firebase from 'firebase';
import { AngularFireDatabase, AngularFireList } from 'angularfire2/database';

@IonicPage()
@Component({
  selector: 'page-confrontation-1',
  templateUrl: 'confrontation-1.html',
})
export class Confrontation_1Page {

  toc_id : string;
  public toc: Array<any> = [];
  public tocRef: firebase.database.Reference = firebase.database().ref('/toc-list');

  constructor(public navCtrl: NavController, public navParams: NavParams, public afDB: AngularFireDatabase) {
    this.toc_id = navParams.get('tocId');
    this.afDB.object('/toc-list/' + this.toc_id).valueChanges().subscribe(res => console.log(res))
  }

  ionViewDidLeave() {
    this.afDB.object('/toc-list/' + this.toc_id).valueChanges().unsubscribe();
  }


}

【讨论】:

    猜你喜欢
    • 2021-11-11
    • 1970-01-01
    • 2021-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-27
    • 2021-05-06
    相关资源
    最近更新 更多