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

132
Visualizações
Cloud Functions document copy

Cloud functions

  1. I want to read the document and edit some fields to add it as a new document. How can I do that?

const newData = value.data(); //<<< field modification

  1. And can I use copyWith ?

Code:

exports.productCopy = functions.https.onCall( async (data, context)=>{

    const uid = context.auth.uid;

 



    data.selectedProductId.forEach((docId){
        const productRef = admin.firestore()
                 .collection('products')
             
                 .doc(docId);
        const newProductRef = admin.firestore()
                         .collection('products')
                  
                         .doc();

        const product = await productRef.get().then((value)=>{
            const newData = value.data();

            newProductRef.add(value.data())
        });



    });


    return {status:'success', isError: false};


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

0

First, is good to know that each document is immutable, after you get a document in a variable you can modify that variable, and then you will need to put/update/patch the new document.

I meant you need to do 2 API calls, to save correctly the document in your database.

about 4 years ago · Juan Pablo Isaza Relatório

0

You can't really use await in forEach block. If you want to wait for multiple actions, use Promise.all().

It should be something like this:

exports.productCopy = functions.https.onCall((data, context) => {
    const uid = context.auth.uid;

    const promises = data.selectedProductId.map((docId) => {
        return admin.firestore() // 👈 Return promise, which bubbles up
          .collection('products')                  
          .doc() // 👈 You need to specify a document ID here, uid maybe?
          .get()
          .then((doc) => {
            const newData = doc.data();
            return admin.firestore() // 👈 Return promise, which bubbles up
                 .collection('products')             
                 .doc(docId)
                 .add(value.data());
          });
    });

    return Promise.all(promises).then((results) => { // 👈 Wait for all promises to resolve
      return { status:'success', isError: false };
    })
});

In general it's best to not mix async/await with then(), as it's easy to get into situations as you had. Here, just using promises keep the code as simple as possible

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