尝试使用“ snapshotChanges”获取键值和数据以表格形式显示,但键以未定义的形式返回,仅数据有效

问题描述

尝试从Firebase数据库中检索数据和键值,但仅数据有效。我试图返回数据和键作为一个对象,但是它不起作用。有人可以帮忙弄清楚如何在html标记的url路径中同时呈现数据和键值吗?

**服务文件

import { AngularFireDatabase } from '@angular/fire/database';
import { Injectable } from '@angular/core';
import { map } from 'rxjs/operators';

@Injectable({
  providedIn: 'root'
})
export class ProductService {

  constructor(private db: AngularFireDatabase) { }

  // insert a new product into product obejct
  create(product) {
    return this.db.list('/products').push(product);
  }

  // get all products from db 
  getAllProducts() {
    return this.db.list('/products').snapshotChanges()
    .pipe(map( action => action
      .map(a => {
        const key = a.payload.key;
        const data = a.payload.val();
        return  data;
      })));
  }
}

** TS文件

import { ProductService } from './../../../services/product.service';
import { Component,OnInit } from '@angular/core';

@Component({
  selector: 'app-admin-products',templateUrl: './admin-products.component.html',styleUrls: ['./admin-products.component.css']
})
export class AdminProductsComponent  {
  
  // Observable
  products$;

  constructor(private productService: ProductService) { 
    this.products$ = productService.getAllProducts();
  }


}

* HTML标记

<table class="table">
  <thead>
    <tr>
      <th>Title</th>
      <th>Price</th>
      <th></th>
    </tr>
  </thead>
  <tbody>
      <tr *ngFor="let p of products$ | async">
          <td>{{ p.title }}</td>
          <td>{{ p.price | currency:'USD':true}}</td>
          <td>
              <a [routerLink]="['/admin/products',p.key]">Edit</a>
          </td>
      </tr>
  </tbody>
</table>

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...