Hey folks I asked a separate query in the morning. I didnt get many responses. From there I have spent a ton of time learning the async/await, promises and callbacks to understand where I am going wrong. However I am still a little confused how to go about executing my logic. Here is my requirement
- My input is a string array of this form ["player1", "player2", "player3".... "player10"]
- I am running two for loops. The first for loop is starting from 0 and till end of array and the second for loop is starting from position 1. The idea is to take pairs together. So player 1 & player2 together. then player 1 and player 3, so on and so forth.
All this works fine.
Before the loop ends, i have two other requirements
- I need to go to another mongo collection with the pair of players and check something.
- Based on a certain condition, I need to save each pair in another collection.
I am using mongoose findOne to query. I want this entire loop to run synchronously that is pass each pair at a time, without completing the for loop first. However I dont know how to run that mongoose query synchronously.
I am also using NODE js
Can someone guide me please?
for (let i=0; i<namearray.length-1;){
player1 = namearray[i];
console.log('Hi I am here 4')
console.log("Hi i am Player 1", player1)
for(let j=0; j<namearray.length;){
if(j <= i){
j = i;
j++;
continue;
}
player2 = namearray[j]
console.log('Hi I am here 5')
console.log("Hi i am player 2", player2)
tournamentMatchRegister.findOne({$and:[{$or:[{Player1_Name : player1}, {Player2_Name : player2}]},
{$or:[{Player1_Name : player2}, {Player2_Name : player1}]}]},function(err,data9){
console.log('Hi I am here 6')
//console.log(data9)
console.log("Hi i am ", player1)
console.log("Hi i am ", player2)
//if(data2.Match_Completed === "No"){
var newpotentialmatch = new scheduleRegister({
// Week_Number: weeknumber,
Player1_Name: player1,
// Player1_Team: data2.Player1_TeamName,
// Player1_Seed: data2.PlayerSeedLevel,
Player2_Name: player2,
// Player2_Team: data2.Player2_TeamName,
// Player2_Seed: data2.PlayerSeedLevel
}); newpotentialmatch.save(function(err,data3){
if(err){
console.log(err)
}
})
})
// })
j++
}
i++
}