简体   繁体   English

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

[英]Firestore how to get the collection value from another collection document id is referenced

I have two fire store collection with following reference image . 我有两个火灾商店集合与以下参考图像。 在此输入图像描述 在此输入图像描述 I want to get the firstName and title . 我想获得firstNametitle Here signup_id is referenced from coll-signup document id. 这里signup_id是从coll-signup文档id引用的。 Following code given below what i did. 以下代码给出了我的所作所为。

Model feed.ts 模型feed.ts

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

news feed.component template 新闻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>

news-feed.component.ts 新闻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 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 is possible to do fire store? 这可以做火店吗? I'm newbie in fire store.Please help me. 我是消防店的新手。请帮帮我。 Thanks! 谢谢!

You need to combine two collections. 您需要组合两个集合。

try this code 试试这段代码

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));

For anyone experiencing issues, combining documents in Firebase Cloud Firestore while using Angular6, RxJS 6 and AngularFire v5.0 try the following code 对于遇到问题的任何人,在使用Angular6,RxJS 6AngularFire v5.0时在Firebase Cloud Firestore中合并文档,请尝试以下代码

Model feed.ts 模型feed.ts

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

export interface CollSignup {
  firstName: string;
  mob: string;
}

export interface ColChallange {
  signup_id: string;
  title: string;
}

Service feed.service.ts 服务feed.service.ts

import { AngularFirestore, AngularFirestoreCollection } from '@angular/fire/firestore';
import { Observable, combineLatest } from 'rxjs';
import {flatMap, map} from 'rxjs/operators';
import {ColChallange, CollSignup, Feed} from '../../models/feed';

@Injectable()
export class FeedService {
  colChallangeCollection: AngularFirestoreCollection<ColChallange>;
  feedItem: Observable<Feed[]>;

  constructor(private afs: AngularFirestore) { }

  collectionInitialization() {
    this.colChallangeCollection = this.afs.collection('col-challange');
     this.feedItem = this.colChallangeCollection.snapshotChanges().pipe(map(changes  => {
      return changes.map( change => {
        const data = change.payload.doc.data();
        const signupId = data.signup_id;
        const title = data.title;
          return this.afs.doc('coll-signup/' + signupId).valueChanges().pipe(map( (collSignupData: CollSignup) => {
            return Object.assign(
              {firstName: collSignupData.firstName, signup_id: signupId, title: title}); }
          ));
      });
    }), flatMap(feeds => combineLatest(feeds)));
  }

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

To print all the data 打印所有数据

this.feedItem.forEach(value => {
  console.log(value);
});

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

相关问题 从id上的Firestore集合中按ID检索文档 - retrieve a document by id from collection of firestore on angular Angular 7和firestore,从另一个集合中获取与另一个集合中的每个文档相对应的数据 - Angular 7 and firestore, get data from another collection that corresponds to each document on another collection 从 Firestore 集合中引用的文档访问数据 - access data from document referenced inside firestore collection 如何使用angularfire从另一个集合中获取firestore中的用户名 - how to get a username in firestore from another collection with angularfire 如何从 firebase - Angular 中的集合中获取单个文档 ID - How to get single document ID from collection in firebase - Angular 如何使用其 ID 删除 Firebase/Firestore 集合中的文档? - How to delete a document in a collection in Firebase/Firestore using its id? 如何从FireStore获取收藏? - How can I get a collection from FireStore? Angular - 将 firestore 文档移动到另一个集合问题 - Angular - Moving firestore document to another collection problem 如何使用angular firestore获取集合中的文档列表 - how to get list of document in a collection, using angular firestore 如何在Firestore上获取集合的每个文档的一个字段? - How to get one field of every document of a collection on Firestore?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM