简体   繁体   English

尝试使用键值(角度)从云 Firestore 获取值

[英]Trying to get values from cloud Firestore using keyvalue (Angular)

My firebase database has a collection called users.我的 firebase 数据库有一个名为 users 的集合。 I'm able to access it with the following code:我可以使用以下代码访问它:

in my service:在我的服务中:

  users$ = this.afs.collection<Users[]>('users').valueChanges();

in my component:在我的组件中:

public users = this.firestoreService.users$.pipe(tap(console.log));

when I just do当我只是做

{{users | async | json}}
, I get the object no problem. ,我拿到object没问题。 I'm having trouble iterating through the them using the keyvalue pipe. this is my code: 我在使用键值 pipe 遍历它们时遇到问题。这是我的代码:

 <pre>{{users | async | json}}</pre> <table class="table"> <thead> <tr> <th>Name</th> <th>Email</th> <th>Phone</th> <th>Role</th> </tr> </thead> <tbody *ngIf="users"> <tr *ngFor="let user of users | async | keyvalue"> <td>{{user.value.displayName}} </td> </tr> </tbody> </table>

if I do user.key, I get the key in the array.如果我执行 user.key,我会得到数组中的键。 can't reach the 'inner' keys or values.无法到达“内部”键或值。 this is my user class:这是我的用户 class:

 export interface Users { displayName: string; email: string; phoneNumber: null; role: string; }

Here's a picture of my database structure:这是我的数据库结构的图片: 在此处输入图像描述

You are returning an Observable<Users[]> in users$.您将在 users$ 中返回一个Observable<Users[]> As such when using users interpolated with async pipe, you are already getting an array Users[] which is an iterable.因此,当使用使用异步 pipe 插入的用户时,您已经获得了一个可迭代的数组Users[]

So change your HTML to:因此,将您的 HTML 更改为:

<tr *ngFor="let user of users | async">
   <td>{{user.displayName}} </td>
</tr>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM