Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

262
Visualizações
adding 2 documents to firestore one is using a reference from the other?

I am trying to created 2 docs at ones what first docs id is used in the second. One problems is my code look wired, especially the chaining. I am afraid even if it works that it might be wrong.

Any idea for a more clear refactor? The flow is the following:

  1. signup a user
  2. get his created id created a new collection
  3. get the new collection id then
  4. create the final doc with the user id and with id of the previously created collection as a reference in the last doc.
const Signup = async (email: string, password: string) => {
    await createUserWithEmailAndPassword(auth, email, password)
      .then((userCredential) => {
        const user = userCredential.user;
        const colRef = collection(db, 'Shared');
        addDoc(colRef, {
          sharedValue: '',
        }).then((res) =>
          setDoc(doc(db, 'Data', user.uid), {
            sharedId: res.id,
          })
        );
      })
      .catch((error) => {
      console.log(error)
      });
  };
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

As shown in the documentation on adding a document:

In some cases, it can be useful to create a document reference with an auto-generated ID, then use the reference later. For this use case, you can call doc().

import { collection, doc, setDoc } from "firebase/firestore"; 

// Add a new document with a generated id
const newCityRef = doc(collection(db, "cities"));

// later...
await setDoc(newCityRef, data);

So:

await createUserWithEmailAndPassword(auth, email, password)
  .then((userCredential) => {
    const user = userCredential.user;
    const colRef = collection(db, 'Shared');
    const docRef = doc(db, 'Shared');
    setDoc(docRef, {
      sharedValue: '',
    }).then((res) =>
      setDoc(doc(db, 'Data', user.uid), {
        sharedId: docRef.id,
      })
    );
  })
  .catch((error) => {
  console.log(error)
  });
};
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda