Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

370
Views
How to get multiple Doc objects in Firebase 9 in React? and get multiple docs from firestore using its doc id?

how do you find a document by its ID in react?

I'm trying to get each of the document specific ids in my collection in Firestore, but I can't find a way to do it so I can access the properties in the collection so I can properly render my components

[ItemDetailContainer.js]

import React, { useState, useEffect } from "react";
import { useParams } from "react-router-dom";
import ItemDetail from "../ItemDetail/ItemDetail";
import { PRODUCTS } from "../ItemListContainer/Products";
import Spinner from "../ItemListContainer/Spinner";
import firebase, { db } from "../../index";
import { getDoc, doc, addDoc } from "firebase/firestore";

const ItemDetailContainer = () => {
  const [jackets, setJackets] = useState([]);
  const [producto, setProducto] = useState({});
  const { id } = useParams();

  useEffect(() => {
    const singleItem = doc(db, "Products", "YPQGY4euFdaUcCoDm06C");

    getDoc(singleItem).then((rta) => {
      console.log(rta.data());
      console.log(rta.id);

      setProducto({ ...rta.data(), id: rta.id });
    });
  }, []);

  console.log(producto);

  return (
    <>
      <div>
        {producto.id ? (
          <>
            <ItemDetail producto={producto} />
          </>
        ) : (
          <div>
            <Spinner />
          </div>
        )}
      </div>
    
    </>
  );
};

export default ItemDetailContainer;
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can use in operator in a query that combines up to 10 equality (==) clauses on the same field with a logical OR and then use getDocs() to get all matching documents:

import { query, where, collection, documentId } from "firebase/firestore";

const colRef = collection(db, "Products");
const q = query(colRef, where(documentId(), 'in', ['docId1', 'docId2']));

getDocs(q).then((snap) => {
  console.log(snap.docs.map(d => ({ id: d.id, ...d.data() }))
})

If you have an array of more than 10 IDs then you can individually query the documents.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!