0

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

2
  • You want to query all documents from project_vendors and then take some fields from all of them and add where? Can you share screenshot of all 3 collections to show sample documents and then explain ? Commented Jul 12, 2022 at 16:28
  • Thanks for responding @Dharmaraj. I added more details. Please let me know if that makes sense. Commented Jul 12, 2022 at 16:44

1 Answer 1

2

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;
  });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.