title: Meteor Server-Side HTTP Request
body: <p>I'm having some issues with a small meteor app I'm trying to build. I have a feeling its a result of not fully understanding how the server-client relationship works. I've tried to get it working for hours, and see this as the only logical way to set it up. It might help to add that I am a beginner.</p>
  
  <p>It is also worth noting that I had it running fine while making the http request on the client side.</p>
  
  <p>What is supposed to happen: </p>
  
  <p>A form is submitted, the text from the form is sent to an API via an http request, JSON is returned, parsed, and a value is returned back to the user (they give a country code, it returns a border). Along with this, I wanted to store each request in a collection with a timestamp. </p>
  
  <p>Any help would be greatly appreciated.</p>
  
  <p>Here is JS:</p>
  
  <pre><code>borders = new Mongo.Collection("borders");
  
  if (Meteor.isClient) {
    // This code only runs on the client
  
    //set the session blank before submit action on form. 
  
    Template.hello.helpers({
      borders: function () {
        // return borders.find({});
        // return borders.find({}, {limit: 1});
        return Session.get("border");
      }
    });
  
    Template.body.events({
      "submit .new-task": function (event) {
        // Prevent default browser form submit
        event.preventDefault();
  
        // Get value from form element
        var countryCode = event.target.text.value;
  
        //set form element variable in the session so it can be accessed on the server
        session.set(countryCode)
  
        //invoke the server method
        Meteor.call("getID", function(error, results) {
          console.log(results.content); //results.data should be a JSON object
        });
  
      }
    });
  }
  
  //server-side code 
  if (Meteor.isServer) {
        session.get(countryCode)
        var url = "http://restcountries.eu/rest/v1/alpha/"+countryCode;
      Meteor.methods({
          getID: function () {
            this.unblock();
            HTTP.get(url, {timeout:30000}, function(error, response) {
              var respJson = JSON.parse(response.content);
              console.log(respJson)
              Session.set("border",respJson["subregion"])
              // Insert a task into the collection
              borders.insert({
                text: respJson["borders"],
                createdAt: new Date() // current time
              });
              // Clear form
              event.target.text.value = "";
            });
          }
      });
  }
  </code></pre>
  
  <p>Error I get when the app is run:</p>
  
  <pre><code>=&gt; Started proxy.                             
  =&gt; Started MongoDB.                           
  W20151203-17:09:54.345(-8)? (STDERR)          
  W20151203-17:09:54.346(-8)? (STDERR) /Users/me/.meteor/packages/meteor-tool/.1.1.10.1evms9b++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/dev_bundle/server-lib/node_modules/fibers/future.js:245
  W20151203-17:09:54.347(-8)? (STDERR)                        throw(ex);
  W20151203-17:09:54.347(-8)? (STDERR)                              ^
  W20151203-17:09:54.347(-8)? (STDERR) ReferenceError: session is not defined
  W20151203-17:09:54.347(-8)? (STDERR)     at borderApp.js:38:7
  W20151203-17:09:54.347(-8)? (STDERR)     at /Users/me/Dev/Web/borderApp/.meteor/local/build/programs/server/app/borderApp.js:67:4
  W20151203-17:09:54.347(-8)? (STDERR)     at /Users/me/Dev/Web/borderApp/.meteor/local/build/programs/server/boot.js:242:10
  W20151203-17:09:54.347(-8)? (STDERR)     at Array.forEach (native)
  W20151203-17:09:54.347(-8)? (STDERR)     at Function._.each._.forEach (/Users/me/.meteor/packages/meteor-tool/.1.1.10.1evms9b++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/dev_bundle/server-lib/node_modules/underscore/underscore.js:79:11)
  W20151203-17:09:54.347(-8)? (STDERR)     at /Users/me/Dev/Web/borderApp/.meteor/local/build/programs/server/boot.js:137:5
  </code></pre>
  
  <p>This is the HTML I am using for the front end:</p>
  
  <pre><code>&lt;head&gt;
    &lt;title&gt;Todo List&lt;/title&gt;
        &lt;meta name="viewport" content="width=device-width, initial-scale=1"&gt;
      &lt;link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"&gt;
      &lt;link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css"&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
    &lt;div class="form-group"&gt;
      &lt;header&gt;
        &lt;h1&gt;Todo List&lt;/h1&gt;
  
        &lt;form class="new-task"&gt;
          &lt;input class="input-lg form-control" type="text" name="text" placeholder="Type to add new tasks" /&gt;
        &lt;/form&gt;
      &lt;/header&gt;
  
  &lt;H3&gt;
  {{&gt; hello}}
  &lt;/h3&gt;
  
    &lt;/div&gt;
  &lt;/body&gt;
  
  &lt;template name="hello"&gt;
    &lt;p&gt;{{borders}}&lt;/p&gt;
  &lt;/template&gt;
  
  &lt;template name="button"&gt;
   &lt;button type="submit" class="btn btn-danger"&gt;Find Borders &amp;rarr;&lt;/button&gt;
   &lt;/template&gt;
  </code></pre>