javascript - 异步等待不等待
问题描述
我希望我的代码等待并让jsonObj.forEach...
通过 2 个 mongodb 查询,然后转到下一个(console.log(branch)
)。为此,我使用 async/await 但即使在那之后我也没有得到所需的结果,因为课程和分支数组打印为空。
jsonObj.forEach(async(data)=>{
try {
var result = await collections.collection('studentData').find({rollNumber:parseInt(data.rollNumber)}).toArray()
console.log('--------data---------')
if (result[0]) {
course.push(result[0].course)
branch.push(result[0].branch)
console.log('-------- entry object ----------')
await collections.collection('something').insertOne(something,(err,res)=>{if (err){console.log(err)}else {console.log('inserted')}})
} else {
console.log('not inserted')
}
} catch (e) {
console.log(e)
}
}
)
var title = 'Result is out'
var body = 'Result of' + reqs.body.company
console.log('-------------branch and course--------------')
console.log(branch)
console.log(course)
知道有什么问题吗?以及如何获得所需的结果。
解决方案
推荐阅读
- python - 无法在 django 项目中创建过滤器
- reactjs - 如何使用 SpeedDial 上传文件?
- python - Numpy - 有没有办法指定广播维度?
- python - 如何将 3D 数组的 python 列表转换为特定形状的单个 3D 数组?
- c - 如何将二维用户输入的数组从函数传递到 main()?
- serverless-framework - 具有 API 网关、Lambda 层和 Webpack 的无服务器框架(配置地狱)
- javascript - JavaScript - 在 ECharts 中显示带有字典的条形图
- function - 函数并从输入中分配 10 个随机数
- c# - 用于识别负数和正数偶数/奇数的代码不起作用
- google-sheets-formula - 如何在谷歌表上将多个复杂的 IF 语句组合成一个查询/公式?