Mike Buzaki
Mike Buzaki

Reputation: 35

Firebase function - how to create multiple documents from different collections

I am fairly new to writing functions and cannot figure out how to solve my issue or even search for it properly.

I have three collections:

current_projects

vendors

vendor_projects

when a new project is created I want the function to take all documents in vendors, add certain fields from them to vendor_projects, and include the project_id field that is created in current_projects.

Do I need a for loop to accomplish this or is there other syntax that could be utilized?

My current function is below. This creates on document using the new project_id field but doesnt take any of the fields from vendors. Any input is greatly appreciated.

exports.createProjVen = functions.firestore.document("/Current_projects/{id}")
.onCreate((snap, context)=>{
  console.log(snap.data());
  const id = snap.data().id;
  // const collection = context.params.Current_projects;
  // const id = context.params.id;

  const projectVendors = admin.firestore().collection("project_vendors");
  // const vendors = admin.firestore().collection("vendors");

  return projectVendors.doc(id).set({
    actual: "",
    budget: "",
    id: "23121",
    project_id: id,
    toggle: "true",
    type: "Fixtures",
    vendor_company: "tes",
    vendor_contact: "tes",
    vendor_email: "[email protected]",
    vendor_id: "test",
    vendor_phone_number: "test"});
});

Adding more details:

When a new project is added it creates a record in current_projects.

enter image description here

I want the function to be able to query all documents in the vendor collection when a new record is created in current_projects.

enter image description here

From there, I want to grab the highlighted fields from the vendor documents, add the id from that was created from current_projects (highlighted in the first screen shot), and create a new document in project_vendors (third screen shot below) for every document in the vendors table.

enter image description here

Upvotes: 0

Views: 556

Answers (1)

Dharmaraj
Dharmaraj

Reputation: 50830

If you are trying to created a document in project_vendors collection for every vendor in vendors after a project is created then you can map an array of promises and then use Promise.all() as shown below:

exports.createProjVen = functions.firestore.document("/Current_projects/{id}")
  .onCreate((snap, context) => {
    const docSnap = snap.data();
    const id = context.params.id;

    const vendorsSnap = await admin.firestore().collection("vendors").get();
    const vendorsData = vendorsSnap.docs.map((d) => ({ id: d.id, ...d.data() }))
    
    const promises = [];
  
    const vendorPrsCol = admin.firestore().collection("project_vendors");
  
    vendorsData.forEach((vendor) => {
      const data = {
        projectId: id,
        email: vendor.email,
        // other vendor fields
      }
      
      promises.push(vendorPrsCol.add(data));
    })

    await Promise.all(promises);
  
    console.log("Vendor Projects added");
    return null;
  });

Upvotes: 2

Related Questions