首页 > 解决方案 > 无法从 firebase 实时数据库中获取键值

问题描述

first.Component.html

<div class="col-md-3">
 <button type="button" routerLink="editlots" class="btn btn-info" style = "font-size: 
         15px; text-align: center; margin:0 auto; display: block;">View Lots
 </button>
</div> 

second.component.html

<tr *ngFor="let it of lot$ | async">
 <td> {{it.alias}} </td>
 <td> {{it.area}} </td>
 <td> {{it.price}} </td>
</tr>

在 plats/bigfork/lots 中,bigfork 是 lot 的父键,但我也有其他键,其中包含与具有相同数据的 lot 相同的子键。所以我想制作一个动态来代替 bigfork,这样我就可以访问关于我点击的特定按钮的数据。

第二个组件.ts

this.itemRef = db.list('/plats/bigfork/lots');
this.lot$ = this.itemRef.snapshotChanges().map(actions => {
    return actions.map(action => ({
        key: action.payload.key,
        ...action.payload.val() 
    }));
});

Firebase 数据库

{
"key": "bigfork",
"bg": "Background URL ",
"cover": "https://",
"description": "The conservation",
"footer": {
  "address": "\t\t",
  "companyName": "Companys",
  "email": "user@email.com",
  "favicon": "https:",
  "phone": "111.1111.111",
  "website": "https://www.facebook.com/#"
},
"header": {
  "logo": "https:",
  "title": "Preserve"
},
"lots": [
  {
    "address": "",
    "alias": "113",
    "area": "",
    "assignedTo": "Available",
  },
  {
    "address": "",
    "alias": "112",
    "area": "",
    "assignedTo": "Available",
  }],

  {
 "key": "cdl",
"bg": "Background URL ",
"cover": "https://",
"description": "The conservation",
"footer": {
  "address": "\t\t",
  "companyName": "Companys",
  "email": "user@email.com",
  "favicon": "https:",
  "phone": "111.1111.111",
  "website": "https://www.facebook.com/#"
},
"header": {
  "logo": "https:",
  "title": "Preserve"
},
"lots": [
  {
    "address": "",
    "alias": "113",
    "area": "",
    "assignedTo": "Available",
  },
  {
    "address": "",
    "alias": "112",
    "area": "",
    "assignedTo": "Available",
  }],

标签: angularfirebaseangularfire2

解决方案


您需要使用查询:

this.itemRef = db.list('/plats', ref => ref.orderByChild('lots').equalTo('lots'))

这样,您可以使用孩子的值来获取数据库中的不同键。


推荐阅读