微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

angular – Firestore如何从另一个集合文档id获取集合值

我有两个火灾商店集合与以下参考图像
0.

enter image description here

enter image description here


我想获得firstName和title.这里signup_id是从coll-signup文档id引用的.以下代码给出了我的所作所为.

模型Feed.ts

export interface Feed {
    firstName? : string;
    signup_id? : string;
    title? : string;
}

新闻Feed.component模板

<ul *ngFor="let Feed of Feeds">
<!-- <li>{{Feed.firstName}}</li> --> // Here I want to print my first name
      <li>{{Feed.title}}</li>
      <li>{{Feed.signup_id}}</li>
    </ul>

新闻Feed.component.ts

import { Component,OnInit } from '@angular/core';
import { Feed } from '../../models/Feed';
import { FeedService } from '../../services/Feed.service';
@Component({
  selector: 'app-news-Feed',templateUrl: './news-Feed.component.html',styleUrls: ['./news-Feed.component.css']
})
export class NewsFeedComponent implements OnInit {
  Feeds : Feed[];
  constructor(private FeedService: FeedService) { }

  ngOnInit() {
    this.FeedService.sellectAllNews().subscribe(Feeds => {
      this.Feeds = Feeds;
    })
  }

}

Feed.service.ts

import { AngularFirestore,AngularFirestoreCollection,AngularFirestoreDocument } from 'angularfire2/firestore';
    import { Observable } from 'rxjs/Observable';
    import { Feed } from '../models/Feed';
    @Injectable()
    export class FeedService {
      FeedCollection : AngularFirestoreCollection<Feed>;
      FeedItem : Observable<Feed[]>;

      constructor(private afs : AngularFirestore) { 
        this.collectionInitialization();
      }

      collectionInitialization() {
// I think here I have to modify or add next collection to will get the output
        this.FeedCollection = this.afs.collection('col-challange');
        this.FeedItem = this.FeedCollection.stateChanges().map(changes => {
          return changes.map(a => {
            const data = a.payload.doc.data() as Feed;
            return data;
          })
        })

      }
      sellectAllNews() {
        this.collectionInitialization();
        return this.FeedItem;
      }
    }

这可以做火店吗?
我是消防店的新手.请帮帮我.谢谢!

解决方法

您需要组合两个集合.

试试这段代码

this.FeedCollection = this.afs.collection('col-challange');
this.FeedItem = this.FeedCollection.snapshotChanges().map(changes => {
      return changes.map(a => {
        //here you get the data without first name
        const data = a.payload.doc.data() as Feed;
        //get the signup_id for getting doc from coll-signup
        const signupId = data.signup_id;
        //get the related document
        return afs.collection('coll-signup').doc(signupId).snapshotChanges().take(1).map(actions => {
          return actions.payload.data();
        }).map(signup => {
          //export the data in Feeds interface format
          return { firstName: signup.firstName,...data };
        });
      })
    }).flatMap(Feeds => Observable.combineLatest(Feeds));

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐