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

255
Vistas
ReactJS: add data to document when creating a subcollection

I need to created documents and subcollections of documents like this:

Structure

So here is my code:

function Subscriptions() {
  useEffect(() => {
    const fetchData = async () => {
      try {
        const userProjectsColRef = collection(db, "users");
        const newProjectDocRef = doc(userProjectsColRef);
        addDoc(collection(newProjectDocRef, "experiences"), {
          name: "hello",
        });
      } catch (err) {
        console.log(err);
      }
    };
    fetchData();
  }, []);
  return <div></div>;
}
export default Subscriptions;

How can I add data to the very first document created (the "data" field with "bio", "fullname", "company") ?

Right now, this code does add a subcollection named "experiences" which has a document with some data (name: "hello"). Like this:

enter image description here

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

0

You need to create two documents: firstly the parent user document and then
the experience document which DocumentReference is based on its parent doc's DocumentReference (i.e. newProjectDocRef).

const userProjectsColRef = collection(db, "users");
const newProjectDocRef = await addDoc(userProjectsColRef, {
  // ...
});
await addDoc(collection(newProjectDocRef, "experiences"), {
  // ...
});

Note that if you want those two writes to be done as an atomic operation, you should use a batched write.

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