从 Firebase 实时数据库(Angularfire 5 和 Ionic 3)中检索单个值

Retrieve single value from Firebase Real Time Database (Angularfire 5 and Ionic 3)

我希望能够使用来自实时数据库 (RTD) 条目的单个值。借助 Firestore,我能够使用以下代码:

interface Led{
  currentState: boolean,
  lightSensor: number
}

currentState: boolean;
lightSensor: number;

this.afs.collection('Led').valueChanges().subscribe((Led: any) => {
  this.lightSensor = Led[0].lightSensor;
  this.currentState = Led[0].currentState;
})

然后我能够将 currentState 或 lightSensor 调用到 HTML 并显示值。我能找到的所有 RTD 示例都使用 ngFor 并显示 JSON 数据,我不知道如何从这些数据中获取单个值。我尝试了以下内容,这些内容基本上是从我的 Firestore 代码中复制的:

this.db.list('leafBox001').valueChanges().subscribe((Led: any) => {
  this.currentState = Led[0].currentState;
  console.log(this.currentState);
});

控制台日志returns未定义。

您可以通过它的键获取对象:

this.db.object(`leafBox001/${id}`).valueChanges()

我认为这只是我对这门语言不熟悉。这是我一直在寻找的答案

遵循方法示例:

getCategory(idCategoria:string) {
  this.refCategoria = this.db.list('categorias',

  ref => ref.orderByChild('id').equalTo(idCategoria));

  this.refCategoria.valueChanges().subscribe(

    (datas) => { console.log("datas", datas) }
  );

}

这是过滤器:

ref.orderByChild('id').equalTo(idCategoria))

'id' => 我的集合字段用于索引数据。 'idCategoria' => 这是我的 RTDB 中注册表的 "id"。