使用node.js和graphql-I';I’我试图显示每所学校的学生总数,但根据Promise.all(),它返回空 async studentCount(@Parent()school:school):承诺{ 让学生ID=[] 让cohortstudentId=[] //查找每个学校的所有课程 const programs=wait this.programService.find({schoolId:school.id}) //查找每个程序的所有队列 programs.map(异步程序=>{ //收集阵列中每个队列的学生ID const cohortsWithStudents=wait this.cohortService.getStudentsForCohortsByProgramId(program.id) //通过循环每个队列找到所有学生,并保存在CohortStudentId[] cohortsWithStudents.map(异步(队列)=>{ wait cohortstudents.map(异步(学生)=>{wait cohortStudentIds.push(学生id)}) }); //将所有学生id数组收集到一个大数组中 等待[ …学生们, …同住学生 ] }) 等待承诺。所有([项目、合作伙伴、学生ID]) 。然后((值)=>{ console.log(值) 返回值 }); console.log('xxx') //返回每所学校的学生人数 返回uniq(studentId).长度 }
您正在传递一个使用node.js和graphql-I';I’我试图显示每所学校的学生总数,但根据Promise.all(),它返回空 async studentCount(@Parent()school:school):承诺{ 让学生ID=[] 让cohortstudentId=[] //查找每个学校的所有课程 const programs=wait this.programService.find({schoolId:school.id}) //查找每个程序的所有队列 programs.map(异步程序=>{ //收集阵列中每个队列的学生ID const cohortsWithStudents=wait this.cohortService.getStudentsForCohortsByProgramId(program.id) //通过循环每个队列找到所有学生,并保存在CohortStudentId[] cohortsWithStudents.map(异步(队列)=>{ wait cohortstudents.map(异步(学生)=>{wait cohortStudentIds.push(学生id)}) }); //将所有学生id数组收集到一个大数组中 等待[ …学生们, …同住学生 ] }) 等待承诺。所有([项目、合作伙伴、学生ID]) 。然后((值)=>{ console.log(值) 返回值 }); console.log('xxx') //返回每所学校的学生人数 返回uniq(studentId).长度 },node.js,promise,async-await,Node.js,Promise,Async Await,您正在传递一个异步函数作为对map()的回调,这将产生一系列承诺,但您永远不会等待这些承诺 相比之下,您正在等待许多不需要等待的事情,因为它们不是承诺,例如推送的返回值、map的返回值或数组文本 你应该写一些类似的东西 async studentCount(@Parent() school: School): Promise<number> { let studentIds = [] let cohortStudentIds = []
异步
函数作为对map()
的回调,这将产生一系列承诺,但您永远不会等待这些承诺
相比之下,您正在等待许多不需要等待的事情,因为它们不是承诺,例如推送的返回值、map的返回值或数组文本
你应该写一些类似的东西
async studentCount(@Parent() school: School): Promise<number> {
let studentIds = []
let cohortStudentIds = []
// find all the programs for each school
const programs = await this.programService.find({ schoolId: school.id })
// find all the cohorts for each program
programs.map(async program => {
//collect the student ids per cohort within array
const cohortsWithStudents = await this.cohortService.getStudentsForCohortsByProgramId(program.id)
// find all the students by looping through each cohort and save in cohortStudentIds[]
cohortsWithStudents.map(async (cohort) => {
await cohort.students.map(async (student) => { await cohortStudentIds.push(student.id) })
});
//collect all the student id arrays into 1 big array
studentIds = await [
...studentIds,
...cohortStudentIds
]
})
await Promise.all([programs, cohortStudentIds, studentIds])
.then((values) => {
console.log( values )
return values
});
console.log('xxx')
// return the number of students per school
return uniq(studentIds).length
}
async studentCount(@Parent()school:school):承诺{
让studentId=新集合
const programs=wait this.programService.find({schoolId:school.id})
等待Promise.all(programs.map)(异步程序=>{
// ^^^^^^^^^^^^^^^^^
const cohortsWithStudents=wait this.cohortService.getStudentsForCohortsByProgramId(program.id)
for(与学生共住的const队列){
for(队列的const student.students){
studentId.add(student.id)
}
}
});
返回studentId.size
}
async studentCount(@Parent() school: School): Promise<number> {
let studentIds = new Set
const programs = await this.programService.find({ schoolId: school.id })
await Promise.all(programs.map(async program => {
// ^^^^^^^^^^^^^^^^^
const cohortsWithStudents = await this.cohortService.getStudentsForCohortsByProgramId(program.id)
for (const cohort of cohortsWithStudents) {
for (const student of cohort.students) {
studentIds.add(student.id)
}
}
});
return studentIds.size
}