【发布时间】:2013-03-04 22:52:57
【问题描述】:
我正在尝试在 2 teams 之间保存 match,我正在通过下拉列表传递 2 teams。
当我在Team.findByKey 方法内使用util.log 将homeTeam 输出到控制台时,它成功运行,输出如下:
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检索Team。matchForm传递给此函数,key用于home和away团队,我想使用这两个键来查找 2 个团队并将它们存储在一个新的Match为 @987654344 @ 和awayteam。我在这里使用了对Team架构的引用,当我执行findByKey方法并输出到控制台 INSIDE 时,它输出到控制台,是的,这是Liverpool,例如它找到了正确的Team.但是在这个方法之外,如果我输出结果,我会得到[object Object]。 -
@JohnnyHK 希望这会有所帮助,我还尝试在尝试保存匹配后输出匹配,并且
hometeam被保存为undefined,因为它不知道这个team是什么。我被困住了,因为我想找到home和away团队,以便将它们保存到相同的match。
标签: javascript mongoose