首页 > 解决方案 > 无法从 firebase-Angular http://localhost:4200/admin/products/undefined 获取 $key

问题描述

我在学习课程的同时从事 Angular 项目。我正在尝试从我的 Firebase 中获取 $key,并在路由中使用它来重定向到编辑页面,但我不断收到“未定义”。我从这里尝试了多种解决方案,但没有任何效果。

我的产品服务。

create (product) {
return this.db.list('/products').push(product);

}

 getAll() {
return this.db.list('/products').valueChanges();

}

 get(productId) : Observable<any> { return this.db.object('/products/' + productId).snapshotChanges();

}

我想在哪里使用 $key。

 <a [routerLink]="['/admin/products/',p.$key]"> Edit </a>

我在浏览器中得到的:http://localhost:4200/admin/products/undefined

标签: angularfirebasekey

解决方案


your best bet would be to try as an example like so - set your variable from firebase or firestore as products in your yourcomponent.component.html file:
<div *ngFor="let item of products | keyvalue">
 <a [routerLink]="['/admin/products/',item.key]"> Edit </a>
</div>

keyvalue 管道由 angular 专门用于分段值和键,并且与 ngfor 配合得很好。


推荐阅读