更新: 我的问题的根源是,我正在通过其uuid向数组分配值。因此,即测验[uuid] = x。因此,数组长度为0,并且该属性显然未在json响应中进行处理。
我正在构建一个函数,在该函数中初始化对象测验并在for循环内对其进行更改。当我完成后,我想兑现诺言。当我调用此函数时,我仅收到初始化值,并且缺少for循环的更改。 console.log(quiz)记录更改的对象。 我找不到我的错误...
我尝试了一些if子句,仅在完成for循环时才调用resolve函数。这导致相同的结果或没有响应。
app.get('/playQuiz', function (req, res) {
playQuiz().then(data => {
res.status(200).json({
quiz: data
});
});
});
async function playQuiz(){
var players = {
puuids: [],
stats: []
}
credentials = await getCredentials();
token = credentials.token;
port = credentials.port;
players.puuids = await getLobby(port, token);
for(i=0; i<players.puuids.length; i++){
players.stats[i] = await getStats(port, token, players.puuids[i])
}
let quiz = await evaluateQuiz(players)
console.log(quiz); // This displays the object quiz with the changes from the loop
return quiz; //The response i get displayed in Postman from this line, does not show the changes in i.e. csScore. It only shows an empty array.
}
function evaluateQuiz(players){
return new Promise((resolve, reject) => {
var quiz = [{
questionId: 1,
question: "Who has the most cs per game?",
csScore: []
},
{
questionId: 2,
question: "Who has the highest vision score per game?",
vsScore: []
},
{
questionId: 3,
question: "Who has the best win rate?",
winRate: []
},
{
questionId: 4,
question: "Who has the most quadra kills?",
quadraKills: []
},
{
questionId: 5,
question: "Who has the highest objective participation?",
objectiveRate: []
}
]
for(var i=0; i<players.puuids.length; i++){
quiz[0].csScore[players.puuids[i]] = players.stats[i].csScore / players.stats[i].gamePlayed;
quiz[1].vsScore[players.puuids[i]] = players.stats[i].visionScore / players.stats[i].gamePlayed;
quiz[2].winRate[players.puuids[i]] = players.stats[i].victory / players.stats[i].gamePlayed;
quiz[3].quadraKills[players.puuids[i]] = players.stats[i].quadraKills / players.stats[i].gamePlayed;
quiz[4].objectiveRate[players.puuids[i]] = players.stats[i].objectiveTakenInvolved / players.stats[i].gamePlayed;
}
//console.log(quiz);
resolve(quiz);
})
};
答案 0 :(得分:2)
在这种情况下,执行异步处理时会使用Promises和callbacks,因为您所做的只是一个简单的计算,不需要使用promises。
这将是您的代码的解决方案,欢迎使用StackOverflow并祝您编程愉快!
Dim objMyList As ListObject
Dim objWksheet As Worksheet
Dim strSPServer As String
Const SERVER As String = "https://twdc.sharepoint.com/sites/WDPR-dclrecruiting/Test/TEP/Trip%20Event%20Planning%20Library"
Const LISTNAME As String = "{6B39FDF1-29AE-418C-9D99-92293FED5C81}"
Const VIEWNAME As String = "{CCFD1C7F-74CA-4921-A599-628C800C818A}"
strSPServer = "http://" & SERVER & "/_vti_bin"
Set objWksheet = Worksheets.Add
Set objMyList = objWksheet.ListObjects.Add(xlSrcExternal, _
Array(strSPServer, LISTNAME, VIEWNAME), False, xlYes, Range("A1"))
阅读文章,以便您更好地了解Promises
的用法