参加者の予測をループしていくつかのポイントを計算したいと思います。 プレイヤーが獲得するポイント数を知る(mongooseを使用して)mongodbを呼び出す必要があります。私はこのコードを実行するとhttp://caolan.github.io/async/async foreach call not synchronous node
var calculateTeamPredictionsPerRound = function (roundId) {
var roundScores = [];
predictions.find({}, {}).exec(function (err, predictions) {
console.log("predictions length: " + predictions.length)
if (err) return console.error(err);
async.each(predictions,
function (prediction) {
var teamScores = [];
console.log("team length: " + prediction.Team.length + "deelnemer: " + prediction.Participant.Name)
async.forEach(prediction.Team, function (player,callback) {
console.log("for player: " + player.PlayerName + "participant: " + prediction.Participant.Name);
teamScore.findOne({ RoundId: roundId, 'Player.Id': player.Id }, { Player: { $elemMatch: { Id: player.Id } } }).exec(function (err, player) {
console.log(player.Player[0].Name);
var playerScore = new Object;
playerScore.Name = player.Player[0].Name;
playerScore.Team = player.Player[0].Team;
playerScore.Won = setWinScore(player.Player[0]);
playerScore.Draw = setDrawScore(player.Player[0]);
playerScore.Played = setPlayedScore(player.Player[0]);
playerScore.RedCard = setRedCardScore(player.Player[0]);
playerScore.YellowCard = setYellowCardScore(player.Player[0]);
playerScore.Assist = setAssistScore(player.Player[0]);
playerScore.Goals = setGoalScore(player.Player[0]);
playerScore.OwnGoal = setOwnGoalScore(player.Player[0]);
playerScore.TotalScore = playerScore.Won + playerScore.Draw + playerScore.Played + playerScore.RedCard + playerScore.YellowCard + playerScore.Assist + playerScore.OwnGoal + playerScore.Goals;
teamScores.push(playerScore)
});
callback();
});
});
});
};
コンソールが私にこのことを示しています:
team length: 11 participant: test
for player: a participant: test
for player: b participant: test
for player: c participant: test
for player: d participant: test
for player: e participant: test
for player: f participant: test
for player: g participant: test
for player: h participant: test
for player: i participant: test
for player: j participant: test
for player: k participant: test
team length: 11participant: test2
for player: a participant: test2
for player: b participant: test2
for player: c participant: test2
for player: d participant: test2
for player: e participant: test2
for player: f participant: test2
for player: g participant: test2
for player: h participant: test2
for player: i participant: test2
for player: j participant: test2
for player: k participant: test
a
b
c
d
e
f
g
h
i
j
k
a
b
c
d
e
f
g
h
i
j
k
その後、私は参加者ごとのスコアを保存したい
私はこの非同期モジュールを使用しています(後のことを行います)
しかし、私がしたい:
team length: 11participant: test
for player: a participant: test
a
for player: b participant: test
b
私はこのモジュールを使用しています:http://caolan.github.io/async/ –
async.forEachはそのモジュールの一部ではないので、わかりました。async.eachだけです。解決策は同じでなければなりません。 – marton
'forEach'は' each'のエイリアスです – megawac