0

私はmatch2の間を保存しようとしています、私はドロップダウンリストを介してteams2を渡します。teams

メソッド内でをコンソールに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]

私の問題は、最終的にホームチームとアウェイチームの両方を1回のセーブで同じ試合にセーブしたいということです。一致を保存するためのコード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);
    });

};

/ save / match

  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の値を使用しています。次のように、ホームチームとアウェイチームの両方のコールバックを内部で作成する必要があります。homeTeamMatchMatchfindByKey

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 に答える