I'm trying to display the list of products in a tabular format. Although I am getting table headings but I am unable to retrieve data from the firebase.
我正在尝试以表格形式显示产品列表。虽然我得到表标题,但我无法从firebase检索数据。
admin-products.component.html
Admin-products.component.html
<p>
<a routerLink="/admin/products/new" class="btn btn-primary"> New Product</a>
</p>
<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.payload.val().title }}</td>
<td>{{ p.payload.val().price }}</td>
<td>
<a [routerLink]="['/admin/products/', p.key]">Edit</a>
</td>
</tr>
</tbody>
</table>
product.service.ts
Product.service.ts
import { Injectable } from '@angular/core';
import { AngularFireDatabase } from '@angular/fire/compat/database';
@Injectable({
providedIn: 'root'
})
export class ProductService {
constructor(private db: AngularFireDatabase) { }
create(product) {
this.db.list('/products ').push(product);
}
getAll() {
return this.db.list('/product').snapshotChanges();
}
}
admin-product.component.ts
Admin-product.component.ts
import { Component } from '@angular/core';
import { ProductService } from 'src/app/product.service';
@Component({
selector: 'app-admin-products',
templateUrl: './admin-products.component.html',
styleUrls: ['./admin-products.component.css']
})
export class AdminProductsComponent {
products$:any;
constructor(private productService: ProductService){
this.products$ = this.productService.getAll();
}
}
exported database json flle
导出的数据库JSON Flle
{
"categories": {
"action": {
"name": "Action"
},
"adventure": {
"name": "Adventure"
},
"fighting": {
"name": "Fighting"
},
"puzzle": {
"name": "Puzzle"
},
"strategy": {
"name": "Strategy"
}
},
"products ": {
"-NdjCd4DNpztAOwiR1Hk": {
"category": "Action",
"imageUrl": "https://cdn.cloudflare.steamstatic.com/steam/apps/1888160/ss_549f55589c10866bc31243d277324e31ad155b29.600x338.jpg?t=1694077391",
"price": 3599,
"title": "ARMORED CORE™ VI FIRES OF RUBICON™"
},
"-Ndu1DegkAnM2FWqx1tu": {
"category": "Action",
"description": "In space, no one can hear you scream … except your friends! Rally your crew, man your spaceship, and prepare for an epic adventure. Battle fearsome foes, salvage valuable loot, die repeatedly - while reclaiming humanity's lost territory together.",
"imageUrl": "https://cdn.cloudflare.steamstatic.com/steam/apps/1063420/header.jpg?t=1694177016",
"price": 2999,
"title": "Void Crew"
}
Pic for the webpage.
(https://i.stack.imgur.com/eiYtE.png)
该网页的图片。(https://i.stack.imgur.com/eiYtE.png))
更多回答
@PeterHaddad console.log(this.products$) returns undefined in console.
@PeterHaddad console.log(this.products$)在控制台中返回未定义。
Try moving the assignment to ngOnInit, injected services aren't available in the constructor, assuming your console logging there.
尝试将赋值移动到ngOnInit,假设您的控制台登录到该构造函数中,注入的服务在该构造函数中不可用。
Screenshots of data are not acceptable here. Edit an excerpt of the data into your question.
这里不接受数据的屏幕截图。在你的问题中编辑数据的摘录。
You don't seem to have a product
key in the database, instead only a products
key. Therefore change the getAll()
method to the following:
您的数据库中似乎没有产品密钥,而只有一个产品密钥。因此,将getall()方法更改为以下内容:
getAll() {
return this.db.list('/products ').snapshotChanges();
.pipe(map( action => action
.map(a => {
const key = a.payload.key;
const data = a.payload.val();
return data;
})));
}
Then in the template do the following:
然后在模板中执行以下操作:
<tbody>
<tr *ngFor="let p of products$| async">
<td>{{ p.title }}</td>
<td>{{ p.price }}</td>
<td>
<a [routerLink]="['/admin/products/', p.key]">Edit</a>
</td>
</tr>
</tbody>
更多回答
我是一名优秀的程序员,十分优秀!