title: Meteor / Mongo - Retrieving _id from template to use in function
body: <p>I am new to programming so apologies if this seems obvious.  I've spent quite some hours on this before asking so I hope this is ok.</p>
  
  <p>I am trying to retrieve the ID of a mongo document so I can update it (via a form).</p>
  
  <p>I will show what I have so far:</p>
  
  <p>Iron Router takes me to the page via a URL:</p>
  
  <pre><code>Router.route('companyDetails', {
  name: 'companyDetails',
  template: 'companyDetails'
  </code></pre>
  
  <p>});</p>
  
  <p>Template that displays the mongo document (it does work):</p>
  
  <pre><code>&lt;template name = "companyDetails"&gt;
  &lt;h3&gt;Update your Company Details&lt;/h3&gt;
  &lt;form class = "companysave"&gt;
      &lt;dl&gt;
          {{#each company}}
              id is: {{_id}}&lt;br&gt;
              &lt;p&gt;&lt;input type = "text" value="{{name}}" name="companyName" size="35"&gt;&lt;/p&gt;
              &lt;p&gt;&lt;input type = "text" placeholder="{{phone}}" name="companyPhone" size="35"&gt;&lt;/p&gt;
              &lt;p&gt;&lt;input type = "text" placeholder="{{web}}" name="companyWeb" size="35"&gt;&lt;/p&gt;
              &lt;p&gt;&lt;input type = "text" placeholder="{{twitter}}" name="companyTwitter" size="35"&gt;&lt;/p&gt;
              &lt;p&gt;&lt;input type = "text" placeholder="{{facebook}}" name="companyFacebook" size="35"&gt;&lt;/p&gt;
          {{/each}}
      &lt;/dl&gt;
      &lt;p&gt;&lt;input type="submit" value="Reset"&gt;
      &lt;input type="submit" value="Update"&gt;&lt;/p&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p></p>
  
  <p>Template helper to send document data to template:</p>
  
  <pre><code>Template.companyDetails.helpers({
      'company': function(){
          return Organisations.find();            
      }
  })
  </code></pre>
  
  <p>Template that is trying to get the ID of the document to send the updated info to a method on the server:</p>
  
  <pre><code>Template.companyDetails.events({
  
      'submit form': function(event){
          event.preventDefault();
  
          var documentId = this._id;
  
          var name = $('[name=companyName]').val();
          var web = $('[name=companyWeb]').val();
          var twitter = $('[name=companyTwitter]').val();
          var facebook = $('[name=companyFacebook]').val();
          Meteor.call('updateCompanyDetails', documentId, name, web, twitter, facebook);
      }
  
  });
  </code></pre>
  
  <p>I also have a server method but I'm currently stuck at the above function in the template event.</p>
  
  <p>The error I receive is based on:</p>
  
  <pre><code>var documentId = this._id;
  </code></pre>
  
  <p>It comes through as undefined.   Interestingly if you look at the template itself I'm writing a bit of debug info called:</p>
  
  <pre><code>    id is: {{_id}}&lt;br&gt;
  </code></pre>
  
  <p>and it shows the correct ID.   So the template has the ID.  It loads and shows the data from the mongo document, but I can't retrieve it from the template event.</p>
  
  <p>Can anyone offer some advice as to what I'm doing incorrectly here?</p>
  
  <p>Again, I am new to this (and programming in general) so it may be a hand slap moment.</p>
  
  <p>Thanks so much,  Rob</p>