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};


});

Solution 1:

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.