Search code examples
jsonangulardatabasegoogle-cloud-firestoreangularfire2

Angular fire : get the keys,values from map in a document as an observable


0th index is number of comments

This is my firestore document . I want to get the list of comments to an observable, subscribe to that and iterate through the comments. How to do that using angular fire ? I am a beginner here so I would be much thankful to you if you could explain this in detail

Thank you !


Solution

  • On your component.ts file

    doc: issue;
    doc_comments: Array<string>;
    ngOnInit() {
        db.collection('Agriculture_and_cropes').doc<issue>('doc_Id').valueChanges().subscribe(data => {
            this.doc = data;
            this.doc_comments = Array.from( data.comments.keys() );
        });
    }
    

    On your component.html

    <ul *ngIf="doc_comments">
        <li *ngFor="let c of doc_comments">
            {{c}} : {{doc.comments[c]}}
        </li>
    </ul>