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

373
Visualizações
Firebase V9 - Custom 'Doc ID' Upon Creation (Javascript)

Im wanting to create a doc in Firebase Firestore with Javascript... That doesn't just have the automatic generated ID

return addDoc(collection(db, "users"), { bio: signupForm['signup-bio'].value });

This is how I'm coding it could someone please show me where or how to add a custom ID to to the doc?

Thanks

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

0

Ok, I figured it out...

To create a custom ID for a new doc is as follows...

setDoc(doc(db, 'collection', 'ID'), { feild: value, feild: value });

or in my case as an example...

setDoc(doc(db, 'users', (cred.user.uid), { bio: signupForm['signup-bio'].value });

This creates a new doc (and the users collection if it doesn't exist already) with the ID being the current logged in users UserID and the data in the doc is the value from a signup form called 'signup-bio'

about 4 years ago · Juan Pablo Isaza Relatório

0

In order to set custom document IDs in Firestore, you need to use the set method instead of add:

db.collection("users").doc("YOUR_ID").set({
    bio: signupForm['signup-bio'].value
});

You can refer to the official documentation on how to Set a document which states that:

To create or overwrite a single document, use the set() method:

// Add a new document in collection "cities"
db.collection("cities").doc("LA").set({
    name: "Los Angeles",
    state: "CA",
    country: "USA"
})
.then(() => {
    console.log("Document successfully written!");
})
.catch((error) => {
    console.error("Error writing document: ", error);
});

If the document does not exist, it will be created. If the document does exist, its contents will be overwritten with the newly provided data, unless you specify that the data should be merged into the existing document...

about 4 years ago · Juan Pablo Isaza Relatório

0

For someone who using collection as variable / existing function.

Example in Typescript like this https://javascript.plainenglish.io/using-firestore-with-typescript-in-the-v9-sdk-cf36851bb099

Normal:

const docRef = doc(helloCol())

But you can also do something like…:

const docRef = doc(helloCol(), 'some-id')
// await setDoc(docRef, { /* data */ })

Without changing helloCol() 🎉

Ref: https://firebase.google.com/docs/reference/js/firestore_.md#doc

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