简体   繁体   English

如何访问 angular 中的 Firestore 集合中的文档字段?

[英]How to access field of a document in firestore collection in angular?

I'm trying to access photoUrl of user on the basis of ID, but unfortunalty am not getting hat is deserved.我正在尝试根据 ID 访问用户的 photoUrl,但不幸的是没有得到帽子。

I have attached a picture of my firestore collection, Please tell me how I can access photoUrl.我附上了我的 Firestore 收藏的图片,请告诉我如何访问 photoUrl。 用户是主要集合,基于ID的文档是文档,每个文档都有一些我想根据ID访问photourl的字段。 User is the main collection, document on the basis of ID are document and each document have some fielld I want to access photourl on the basis of ID.用户是主要集合,基于ID的文档是文档,每个文档都有一些我想根据ID访问photourl的字段。

getImage(id) { this.users=this.db.collection('user').doc(id).collection('photurl').valueChanges(); console.log(this.users); }

I have implemented the code in the answered but facing a new issue, the function continuously calls itself as the resulting picture is not displaying.我已经实现了已回答但面临一个新问题的代码,function 不断调用自身,因为结果图片未显示。

<div *ngFor="let Question of Questions "> <img src="{{ getImage(Question.authorNo) }}" class="d-block ui-w-40 rounded-circle" alt="">

This is my html, I have used iterator because I have many question, I am getting authorID of author and match it with the userID, if any userID matches AuthorID get it photoUrl and display it My function getting photoUrl is as follow这是我的 html,我使用了迭代器,因为我有很多问题,我正在获取作者的 authorID 并将其与 userID 匹配,如果任何 userID 与 AuthorID 匹配,则获取它的 photoUrl 并显示它我的 function 获取 photoUrl 如下

getImage(id) { this.db.doc( user/${id}`).valueChanges().subscribe(user => { this.user = user; this.url = this.user.photoUrl; //console.log(this.url); getImage(id) { this.db.doc( user/${id}`).valueChanges().subscribe(user => { this.user = user; this.url = this.user.photoUrl; //控制台。日志(this.url);

}); }); return this.url;返回这个。url; }` }`

Use a an Observable to subscribe to the user data and assign the photoUrl to a variable [Option 1] or you can then assign all of the user data to a user variable and use user.photoUrl in the template [Option 2].使用 Observable 订阅用户数据并将 photoUrl 分配给变量 [选项 1],或者您可以将所有用户数据分配给用户变量并在模板中使用user.photoUrl [选项 2]。

Option 1选项1

user: any;
photoUrl: string; 

...

ngOnInit(): void {
    this.db.doc(`user/${id}`).valueChanges().subscribe(user => {
        this.user = user;
        this.photoUrl = user.photoUrl;
    });
}

Option 2选项 2

user: any; 

...

ngOnInit(): void {
    this.db.doc(`user/${id}`).valueChanges().subscribe(user => this.user = user);
}

Edit: The data model you have currently doesn't lend an easy option to retrieve the photo url.编辑:您当前拥有的数据 model 无法轻松选择检索照片 url。 It is recommended to save the photo url directly onto the question document like you are doing with the author id to prevent so many extra reads.建议像使用作者 ID 一样将照片 url 直接保存到问题文档中,以防止过多的额外阅读。 Option 3 would allow you to do what you want but it isn't recommended.选项 3 将允许您做您想做的事,但不建议这样做。

Option 3 NOT RECOMMENDED不推荐选项 3

photoData = [];

...

ngOnInit(): void {
    this.db.collection<any>('user').snapshotChanges().subscribe(snapshots => {
      snapshots.forEach(snapshot => {
        const url = snapshot.payload.doc.data().photoUrl;
        const id = snapshot.payload.doc.id;
        const data = { url, id };
        this.photoData.push(data);
      })
    });
}


getImage(id: string) {
  this.photoData.forEach(item => {
    if (item.id === id) {
      return item.url;
    }
  })
}

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

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