【发布时间】:2022-02-15 02:32:28
【问题描述】:
此代码可能无法正常工作,因为与 mongo 的连接建立得不够快。所以我认为我必须将其更改为 asnyc 函数。但绝对不知道从哪里开始。我是 Node 和 Mongo 的新手
| left |
|---|
| Julian Graber 900 1 |
| windowsdemo 673 3 |
| laptopDemo 640 4 |
| IpadDemo 628 5 |
看起来应该是这样,但是 mongo 更新它,每个等级都是相同的数字。
// function for giving rank
function giveRank(arrayArg,resultArg){
// declaring and initilising variables
let rank = 1;
prev_rank = rank;
position = 0;
// displaying the headers in the console
console.log('\n-------OUR RESULTS------\n');
console.log('Name | Mark | Position\n');
// looping through the rank array
for (i = 0; i < arrayArg.length ; i ++) {
/*
If it is the first index, then automatically the position becomes 1.
*/
if(i == 0) {
position = rank;
Ranking.find({name: arrayArg[i]}).then((data) => {
if(data){
updateRank(bla, bla, bla)
}else{
newRank(bla,bla,bla);
}
});
/*
if the value contained in `[i]` is not equal to `[i-1]`, increment the `rank` value and assign it to `position`.
The `prev_rank` is assigned the `rank` value.
*/
} else if(arrayArg[i] != arrayArg[i-1]) {
rank ++;
position = rank;
prev_rank = rank;
Ranking.find({name: arrayArg[i]}).then((data) => {
if(data){
updateRank(bla, bla, bla)
}else{
newRank(bla,bla,bla);
}
});
/*
Otherwise, if the value contained in `[i]` is equal to `[i-1]`,
assign the position the value stored in the `prev_rank` variable then increment the value stored in the `rank` variable.*/
} else {
position = prev_rank;
rank ++;
Ranking.find({name: arrayArg[i]}).then((data) => {
if(data){
updateRank(bla, bla, bla)
}else{
newRank(bla,bla,bla);
}
});
}
}
}
【问题讨论】:
-
“异步函数”是指
async/await?
标签: javascript mongodb asynchronous