Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

448
Vistas
trying to access a collection with uid, firestore

I'm using react with firebase/firestore, I'm moving from Angular to React but I'm struggling to access the firestore database without AngularFirestore. I am able to login to firestore and obtain the user.uid, I just can't access the 'pred' collection which is nested inside each user.uid so this data is only for the user.

In Angular I access my database like this: (it works)

this.auth.user.pipe(take(1)).subscribe((user) => {
      if (user) {
        this.items = this.db
          .collection("users")
          .doc(this.user.uid)
          .collection("pred")
          .valueChanges();

In react I'm trying to do the same collection, doc, collection, but I can't find clear documentation how to do it.

My react attempt:

import React, { useEffect, useState } from "react";
import { useAuthState } from "react-firebase-hooks/auth";
import { useNavigate } from "react-router-dom";
import "./Dashboard.css";
import { auth, db, logout } from "../../services/firebase/firebase-auth";
import { query, collection, getDocs, where, doc } from "firebase/firestore";
function Dashboard() {
    const [user, loading, error] = useAuthState(auth);
    const [name, setName] = useState("");
    const navigate = useNavigate();
    const fetchData = async () => {
        const userId = auth.currentUser?.uid;
        const docRef = collection(db, "users");
        const predictions = collection(db, "pred");
        const predSnapshot = await getDocs(predictions);
        const predList = predSnapshot.docs.map((doc) => doc.data());
        return predList;
    };

How can I return the predList which follows:

.collection("users")
      .doc(this.user.uid)
      .collection("pred") 

path order, but using react or just plain javascript? I'm using this import { query, collection, getDocs, where, doc } from "firebase/firestore"; package but I can't see how to do it.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

To create a reference to the pred subcollection of the current user in the new modular/v9 syntax:

collection(db, "users", userId!, "pred")
about 4 years ago · Juan Pablo Isaza Denunciar

0

const fetchData = async () => {
        const userId = auth.currentUser?.uid;
        const docRef = collection(db, "users", userId as string, "pred");
        const preds = await getDocs(docRef);
        const predList = preds.docs.map((doc) => doc.data());
        console.log(predList);
    };
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda