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

时间:2013-03-03 19:59:01

标签: javascript mongoose

我正在尝试将match保存在2 teams之间,我通过下拉列表传递2 teams

当我使用util.log INSIDE homeTeam方法成功运行时,Team.findByKey输出3 Mar 19:52:33 - { name: 'Liverpool', _id: 51312074bb176ba624000007, __v: 0, key: 1362174068837 } ,输出结果如下:

undefined

但是当我尝试在此方法之外执行此操作时,我得到以下输出,这意味着当我尝试将其保存为匹配时,家庭团队显示为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
});

但我想做的是能够用以下

保存匹配
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);
    });

};

有没有人有任何想法?

以下是相关代码:

的JavaScript

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

    }
  });

/保存/匹配

{{1}}

1 个答案:

答案 0 :(得分: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等流量控制库。