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

103
Visualizações
Genrate doc id in firestore v9 before the doc is created

In firestore v8 (web sdk) I was doing it like below:

firestore.ref().collection("genId").doc().id

But I can't figure out the correct syntax for v9 as collection doesn't have doc() method anymore.

Any suggestions are much appreciated

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

From the Firebase 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);

Instead of writing to the ref as the sample does, you can also get the ID from the document ref with:

console.log(newCityRef.id);
about 4 years ago · Juan Pablo Isaza Relatório

0

You can call collection to get a CollectionReference Object and use this to call doc.

Collection: https://firebase.google.com/docs/reference/js/firestore_.md#collection

Doc: https://firebase.google.com/docs/reference/js/firestore_.md#doc_2

const ref = collection(firestore, "genId")
const id = doc(collection)

As per the API reference:

If no path is specified, an automatically-generated unique ID will be used for the returned DocumentReference.

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