Search code examples
meteormeteor-autoformmeteor-collection2simple-schema

Meteor using a collection.find() in another collection


I need to proceed a collection.find() in another collection. I have a collection named Orders.

Orders = new Mongo.Collection('orders')
Orders.attachSchema(new SimpleSchema({
clientId: {
    type: String
  },
  orderDate: {
    type: Date
  }
});

It uses aldeed:collection2 and aldeed:simple shema. I use it to create a form using aldeed:autoform

I would like to put an allowedValues in the ClientId field that would contain the client ids. So I need to get all clients id with a Clients.find(). Clients is another collection. But on the client if I try to put console.log(Clients.find().fetch()) in my order.js collection file, it logs an empty array while on the server it logs an array containing all my clients. I unterstand that the call to the function is made before the collection is available by the client. But even wrapping it in a Meteor.startup doesn't work. Does someone have a clue?


Solution

  • I guess what you are trying to do is limit the selection of client in the Order editor. You can create a helper like following

    Template.editOrder.helpers
       clientList: ()->
         Client.find().fetch()
    

    And use this helper with autoform

    {{> afQuickField name="clientSelected" options=clientList}}