Consultation of collections firebase

Asked

Viewed 53 times

-1

I have an angular design and I’m using firebase as a database, but I’m having trouble accessing a collection that’s inside another collection. My firebase project is like this:

colection(usuarios) == documents(1" 1 is the token because it only has a registered user there") and within that user with token 1 I have another colection with the name of (publicacoes) that inside it contains the publications of that user. How do I access this Colection from my Cod.

Cod

my service I’m using to test:

import { Injectable } from '@angular/core';
import { AngularFirestore } from '@angular/fire/firestore';


@Injectable()

export class TestService {
  constructor(private firestore: AngularFirestore) { }

  criar_novo_user(record) {
    return this.firestore.collection('usuarios').add(record);
  }
  carregar_todos_usuarios() {
    return this.firestore.collection('usuarios/').snapshotChanges();
  }
}


my component where I am using this service:

import { Component, OnInit } from '@angular/core';
import { FeedService } from './feed.service';
import { Publicacao } from './publi/publi.model';
import { TestService } from '../test.service';


@Component({
  selector: 'app-feed',
  templateUrl: './feed.component.html',
  styleUrls: ['./feed.component.css']
})
export class FeedComponent implements OnInit {

  teste

  constructor(private testeService: TestService) { }

  ngOnInit(){

      this.testeService.carregar_todos_usuarios().subscribe(data => {
        this.teste = data.map(object => {
        return{
          token: object.payload.doc.id,
          id: object.payload.doc.data()['id'],
          user: object.payload.doc.data()['user'],
          nome: object.payload.doc.data()['nome'],
          imagePerfil: object.payload.doc.data()['imagePerfil'],
          seguidores: object.payload.doc.data()['seguidores'],
          seguindo: object.payload.doc.data()['seguindo'],
          bios: object.payload.doc.data()['bios'],
          totalPublicacoes: object.payload.doc.data()['totalPublicacoes']
        };})
        console.log(this.teste);
        })
  }

}

1 answer

-1

You have to make one for by receiving users, thus:

firebase.firestore().collection('usuarios').get().then((docData) => {
  docData.docs.map((e, i, a) => {
    const collectionPublicacoes = firebase.collection('usuarios').doc(e.id);
collectionPublicacoes.get().then((res) => {
 res.docs.map((el, in, ar) => {
  console.log(el.data());
});
});
});
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.