【问题标题】:Mongoose / JavaScript - Saving error - undefined objectMongoose / JavaScript - 保存错误 - 未定义的对象
【发布时间】:2013-03-04 22:52:57
【问题描述】:

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

当我在Team.findByKey 方法内使用util.loghomeTeam 输出到控制台时,它成功运行,输出如下:

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

但是当我尝试在此方法之外执行此操作时,我会得到以下输出,这意味着当我尝试将其保存为比赛时,主队显示为 undefined 而不是 @987654329 的 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);

    }
  });

【问题讨论】:

  • 只是在看这个,但是有点不知所措。你能简化你的问题吗?
  • 很多要读我知道对不起,findByKey 根据传递给它的key 检索TeammatchForm 传递给此函数,key 用于 homeaway 团队,我想使用这两个键来查找 2 个团队并将它们存储在一个新的 Match 为 @987654344 @ 和 awayteam。我在这里使用了对 Team 架构的引用,当我执行 findByKey 方法并输出到控制台 INSIDE 时,它输出到控制台,是的,这是 Liverpool,例如它找到了正确的 Team .但是在这个方法之外,如果我输出结果,我会得到[object Object]
  • @JohnnyHK 希望这会有所帮助,我还尝试在尝试保存匹配后输出匹配,并且hometeam 被保存为undefined,因为它不知道这个team 是什么。我被困住了,因为我想找到 homeaway 团队,以便将它们保存到相同的 match

标签: javascript mongoose


【解决方案1】:

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

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

【讨论】:

  • 是否有另一种方法可以在不使用异步之类的情况下进行合理组织的事情?这对我来说看起来很复杂,看起来像是我认为我做不到的事情。非常感谢您的帮助。
  • @germainelol '简单'的方法是保持嵌套回调,但是学习使用像异步这样的流控制库对于 node.js 来说是一项非常重要的技能,一旦你掌握了它就非常容易使用'它。
  • 感谢您的提示,您能否提供一个使用嵌套回调方法的示例?我暂时会尝试实现它,我需要寻找一些使用异步的教程。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多