Mongoose/JavaScript-保存错误-未定义对象

Mongoose/JavaScript-保存错误-未定义对象,javascript,mongoose,Javascript,Mongoose,我正在尝试保存两个团队之间的匹配,我正在通过下拉列表传递两个团队 当我使用util.log在Team.findByKey方法内将homeTeam输出到控制台时,它会成功工作,以下是输出: 3 Mar 19:52:33 - { name: 'Liverpool', _id: 51312074bb176ba624000007, __v: 0, key: 1362174068837 } 但当我尝试在这个方法之外执行此操作时,我会得到以下输出,这意味着当我尝试将其保存为匹配时,主队显示为未

我正在尝试保存两个
团队之间的
匹配
,我正在通过下拉列表传递两个
团队

当我使用
util.log
Team.findByKey
方法内将
homeTeam
输出到控制台时,它会成功工作,以下是输出:

3 Mar 19:52:33 - { name: 'Liverpool',
  _id: 51312074bb176ba624000007,
  __v: 0,
  key: 1362174068837 }
但当我尝试在这个方法之外执行此操作时,我会得到以下输出,这意味着当我尝试将其保存为匹配时,主队显示为未定义的,而不是主队的id:

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

    }
  });

/save/match
中,您正在
match
构造函数中使用
homeTeam
的值,然后回调将其设置。您需要在主客场球队中创建
比赛
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
    });
  });
});

要并行查找主客场球队,同时仍保持代码的组织性,您需要考虑使用流控制库,如。

刚才看到的,但这有点让人不知所措。你能简化你的问题吗?有很多要读的东西我知道很抱歉,
findByKey
根据传递给它的
检索
团队
matchForm
通过
传递给此函数对于
home
awayteam
团队,我想使用这两个键找到这两个团队,并将它们存储在新的
Match
中,作为
hometeam
awayteam
。我在这里使用了对
团队
模式的引用,当我执行
findByKey
方法并在其中输出到控制台时,它会输出到控制台,例如,这是
利物浦
,并且它找到了正确的
团队
。但是在这个方法之外,如果我输出我得到的结果,
[object object]
@johnyhk希望这有帮助,我也尝试在尝试保存后输出匹配,并且
主队
被保存为
未定义的
,因为它不知道这个
团队
是什么。我被困住了,因为我想找到
主场
客场
团队,以便将他们保存到相同的
比赛
。有没有其他方法可以在不使用异步之类的东西的情况下合理组织?对我来说,这看起来很复杂,我觉得我做不到。非常感谢您的帮助。@germainelol“简单”的方法是保持嵌套回调,但是学习使用像async这样的流控制库对于node.js来说是一项非常重要的技能,并且在“获得”它之后就很容易使用。感谢您的提示,您是否能够提供一个使用您提到的嵌套回调方法的示例?目前我将尝试实现它,我需要寻找一些使用异步的教程。