无法从Firebase实时数据库中获取密钥值

时间:2020-02-25 04:50:14

标签: angular firebase angularfire2

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是手的父键,但是我还有其他键,其中包含与具有相同数据的手相同的子键。因此,我想动态地代替bigfork,以便我可以单击的特定按钮来访问数据。

second.component.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",
  }],

1 个答案:

答案 0 :(得分:0)

您需要使用查询:

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

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