title: How Express/Mongoose populate data to HTML select tag
body: <p>May I ask a stupid question? I have a form having select list that needs to have data from MongoDB. First I try to get data from MongoDB (I use Mongoose), save it to array, and then render to view to populate to HTML Select.</p>
  
  <p>File Task.js</p>
  
  <pre><code>exports.createForm = function (req, res) {
    var creators = [];
  
    CreatorModel.find({}).populate('id name').exec(function(err, crts) {
     console.log("Creators:", crts);
     creators = crts;
    });
  
    res.render('tasks/create-form', {
      creators: creators
    });
  };
  </code></pre>
  
  <p>File Template</p>
  
  <pre><code>&lt;p&gt;
      Creator:
      &lt;select id="creatorlist" name="creator"&gt;
        {{#each creators}}
          &lt;option value="{{id}}"&gt;{{name}}&lt;/option&gt;
        {{else}}
          &lt;option value=""&gt;No creators&lt;/option&gt;
        {{/each}}
      &lt;/select&gt;
      &lt;a href="/creator/new"&gt;New Creator&lt;/a&gt;
    &lt;/p&gt;
  </code></pre>
  
  <p>But If I embed render function into callback, it works</p>
  
  <pre><code>CreatorModel.find({}, function(err, creators) {
      AssigneeModel.find({}, function(err, assignees) {
        res.render('tasks/create-form', {
          creators: creators,
          assignees: assignees
        });
      });
    });
  </code></pre>
  
  <p>But I find that it's not OK, because later I have to display exactly value from task ID and quite difficult to improve code</p>
  
  <p>THen I modify my code a little bit</p>
  
  <pre><code>var creators = {};
  
    CreatorModel.find({}).exec(function(err, crts) {
    console.log("Type:", typeof(crts));
  
     creators = crts;
     console.log("Creators:", creators);
    });
  
  
    console.log("Creators 2:", creators); 
  </code></pre>
  
  <p>I check in console log and find that "Creators 2" printed first</p>
  
  <pre><code>Creators 2: {}
  Type: object
  Creators: [ { name: 'Jen Smith', _id: 51d3ec2933a3c53c57000001, __v: 0 },
    { name: 'Paul Scholes', _id: 51d3ec3933a3c53c57000002, __v: 0 } ]
  </code></pre>
  
  <p>It seems that Mongoose find() run asyn.. </p>
  
  <p>The problem with assigning to array is I can not convert json document obj  to array in callback function? Are there other ways to populate date from MongoDb to HTML Select Tag ?</p>