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
All this works fine.
Before the loop ends, i have two other requirements
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++
}
The for loop will complete unless you break out of it. If I'm understanding what you're trying to do, I think using callbacks would be the way forward. I think some of the answers here may be of help: Making mongoose.js queries run synchronously
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.