angular – Firestore如何从另一个集合文档id获取集合值
发布时间:2020-12-17 06:55:02 所属栏目:安全 来源:网络整理
导读:我有两个火灾商店集合与以下参考图像 0. 我想获得firstName和title.这里signup_id是从coll-signup文档id引用的.以下代码给出了我的所作所为. 模型feed.ts export interface Feed { firstName? : string; signup_id? : string; title? : string;} 新闻feed.co
我有两个火灾商店集合与以下参考图像
0. 我想获得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)); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |