Autoform add data from another collection

in SimpleSchema how can I add a select field with data from another collection? For example, I have a Products Collection and I am attaching it an Schema and in that Schema I need the Collection Vendors in a selectField

Hey @ozcoro, did you already solve your issue?

If not, here’s i did on my app same use case as yours.

You have two options:

First, use helpers:

    Template.myTemplate.helpers( {
      vendorOptions: function () {
       return Vendors.find( {} ).map( function ( c ) {
           return {
              label: c.name,
              value: c._id
          }; 
       } );
      }
    } );

Second, on your Products schema:

    vendor: {
      type: String,
      label: 'Vendor Name',
      autoform: {
      firstOption: 'Select Vendor Name',
      options: function () {
        return Vendors.find( {}, {
          sort: {
          name: 1
         }
         } ).map( function ( c ) {
            return {
              label: c.name,
              value: c._id
           };
       } );
      },
    },

on your template:

  {{> afQuickField name="vendor" }} // option 2 in schema

  {{> afQuickField name="vendor" options=vendorOptions}} // option 1 with helpers

Either or willl yield the same result.

I hope this solves your issue.

1 Like