0

我正在尝试保存matchbetween 2 teams,我正在通过teams下拉列表传递 2 。

当我使用INSIDE方法将其util.log输出到控制台时,它成功运行,这是输出:homeTeamTeam.findByKey

3 Mar 19:52:33 - { name: 'Liverpool',
  _id: 51312074bb176ba624000007,
  __v: 0,
  key: 1362174068837 }

但是,一旦我尝试在此方法之外执行此操作,我就会得到以下输出,这意味着当我尝试将其保存为比赛时,主队显示为只是undefined而不是 id hometeam

3 Mar 19:54:09 - [object Object]

我的问题是,我最终希望在一次扑救中将主队和客队都救到同一场比赛中。保存匹配的代码在Team.findByKey方法中有效,如下所示:

  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");
        }
      });
    });
  });

但我想做的是能够用以下内容保存比赛

var match = new Match({
    hometeam: homeTeam._id,
    awayteam: awayTeam._id
});

有没有人有任何想法?

以下是相关代码:

JavaScript

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);
    });

};

/保存/匹配

  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);

    }
  });
4

1 回答 1

1

/save/match你使用构造函数homeTeam中的值Match之前,它是由回调设置的。您需要在Match内部创建主队和客队findByKey回调,如下所示:

Team.findByKey(homeId, function(err, homeTeam) {
  util.log(homeTeam);
  if(err) {
    return util.log("Error occured");
  }
  if(!homeTeam) { 
    return util.log("The home team does not exist");
  }

  Team.findByKey(awayId, function(err, awayTeam) {
    util.log(awayTeam);
    if(err) {
      return util.log("Error occured");
    }
    if(!awayTeam) { 
      return util.log("The away team does not exist");
    }

    var match = new Match({
      hometeam: homeTeam._id,
      awayteam: awayTeam._id
    });
  });
});

要在保持代码井井有条的同时并行查找主队和客队,您需要考虑使用async之类的流控制库。

于 2013-03-04T14:18:29.987 回答