title: Mongoose / JavaScript - Saving error - undefined object
body: <p>I am trying to save a <code>match</code> between 2 <code>teams</code>, I am passing the 2 <code>teams</code> through a drop down list. </p>
  
  <p>When I use <code>util.log</code> to output the <code>homeTeam</code> to the console when INSIDE the <code>Team.findByKey</code> method it works successfully and here is the output:</p>
  
  <pre><code>3 Mar 19:52:33 - { name: 'Liverpool',
    _id: 51312074bb176ba624000007,
    __v: 0,
    key: 1362174068837 }
  </code></pre>
  
  <p>But as soon as I try to do this outside of this method I get the following output which means that when I try to save this as a match, the hometeam appears as just <code>undefined</code> rather than the id of the <code>hometeam</code>:</p>
  
  <pre><code>3 Mar 19:54:09 - [object Object]
  </code></pre>
  
  <p>My problem is that I am eventually wanting to save both a home team and an away team to the same match in one save. The code for saving a match works when inside the <code>Team.findByKey</code> method which is as follows:</p>
  
  <pre><code>  app.get('/save/matchTest', function(req, res) {
      var key = 1362174006191; // Man Utd 51312036bb176ba624000001
      Team.findByKey(key, function(err, team) {
        util.log(team);
        if(err) {
          util.log("Error occured");
        }
        if(!team) { 
          util.log("The team does not exist");
        }
        var match = new Match({
          hometeam: team._id
        });
        match.save(function(err) {
          if(err) {
            util.log('Error while saving Match: ' + util.inspect(err));
            res.send("An error occured whilst saving the match");
          } else {
            res.send("Saved the match");
          }
        });
      });
    });
  </code></pre>
  
  <p>But what I want to do is to be able to save a match with the following</p>
  
  <pre><code>var match = new Match({
      hometeam: homeTeam._id,
      awayteam: awayTeam._id
  });
  </code></pre>
  
  <p>Does anyone have any ideas?</p>
  
  <p>Here is the relevant code:</p>
  
  <p>JavaScript</p>
  
  <pre><code>submitMatch = function(){
      var homeId = $("#homeTeamList").val();
      var awayId = $("#awayTeamList").val();
  
      //alert("home: " + homeId + " away: " + awayId);
  
      // Frontend sends the data
      var matchForm = {
          homeKey : $('#homeTeamList').val(),
          awayKey : $('#awayTeamList').val()
      };
  
      // Basic validation
      $.post('/save/match', {'matchForm' : matchForm}, function(response) {
          console.log(response);
      });
  
  };
  </code></pre>
  
  <p>/save/match</p>
  
  <pre><code>  app.post('/save/match', function(req, res) {
      util.log('Serving request for url [GET] ' + req.route.path);
      // Output to console to test what is being passed to Save Match
      // Entire body passed to console
      //console.log('body: ', req.body);
      // Entire matchForm from body
      //console.log('matchForm: ', req.body.matchForm);
      // Home Key from matchForm
      //console.log('homeKey: ', req.body.matchForm.homeKey);
      // Away Key from matchForm
      //console.log('awayKey: ', req.body.matchForm.awayKey);
  
      // Get data from match Form
      var matchForm = req.body.matchForm;
  
      // Check if a match with 2 teams has been submitted
      if(matchForm.homeKey === '' || matchForm.homeKey === undefined ||
          matchForm.awayKey === '' || matchForm.awayKey === undefined){
        // Not a valid match
        util.log('Not valid match');
      } else {
        var homeId = matchForm.homeKey;
        var awayId = matchForm.awayKey;
  
        var homeTeam = Team.findByKey(homeId, function(err, homeTeam) {
          util.log(homeTeam);
          if(err) {
            util.log("Error occured");
          }
          if(!homeTeam) { 
            util.log("The home team does not exist");
          }
        });
  
        var match = new Match({
          hometeam: homeTeam._id
        });
  
        //util.log(match);
  
      }
    });
  </code></pre>