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

177
Visualizações
How to check I'm generating an Unique ID?

I'm very aware that when you create a document in firebase it does generate a random unique ID but I want to create my own based on 5 random numbers let id = Math.floor(Math.random() * (99999 + 1));

now my question is how do I check that number I'm generating is not already in the documents I have created? is there a way to check documents ID and then make it randomize another number ?

so far I'm doing it outside because I have no idea how to check that inside my doc.set

I want both my doc.id and the inner value id to be the same

Code for reference:

let id = Math.floor(Math.random() * (99999 + 1));
    const docId = `${id}`;

    const registrarPedido = (e) => {
        e.preventDefault();
        const docRef = db.collection('usuarios').doc(user.uid).collection('pedidos').doc(docId)
        docRef.set({
            
          nombre: nombre,
          grado: grado,
          total: totalPrice,
          escuela: escuela,
          metodoDePago: "ACH",
          estadoDePago: "Pendiente",
          fecha: formattedDate,
          id: docId,
          entrega: "Retirar en Sede"

        }).then((r) => {
            history.push("/Inicio");
        })
      }
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

  1. If you want be sure that it doesnt already exists, you could make a query to the document ID to check if it exists. If not, you can use it.

something like:

admin.firestore().collection('usuarios').doc(nuevo_id).get.then(res=>{

    
   if(!res.data().exists){
    
    admin.firestore.collection('usuarios').doc(nuevo_id).set({ nombre: nombre,
              grado: grado,
              total: totalPrice,
              escuela: escuela,
              metodoDePago: "ACH",
              estadoDePago: "Pendiente",
              fecha: formattedDate,
              id: docId,
              entrega: "Retirar en Sede"
        })

     }
 });
  1. If you plan to use a lot of documents and the probability of using the same ID is big enough, you could make a document with every taken ID and use to generate a new ID.

In this case, I wouldn't use a random ID to make it easier and faster to generate.

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