node.js - Knejx Js 只承诺
问题描述
如何使这个功能工作。只有一个承诺回到我身边。
codeProducts.forEach((code, index) => {
const qt = app.db('products').where('code',code).first().then(result => result.quantity)
data[index] = {
code: code,
quantity: qt
}
})
return data
解决方案
let data = codeProducts.map((code, index) => {
return app.db('products').where('code',code).first()
.then(result => {
return {
code: code,
quantity: result.quantity
}
})
})
return data
此代码应该可以解决您的问题。您正在访问承诺之外的数量。为了在数据数组上设置数量,你需要在 then
推荐阅读
- php - Typo3、DEB.SURY 和 TFS 构建管道:helhum/typo3-console 和 PHP 版本的问题
- yii2 - 检查是否存在一对属性
- android - 如何修复 java.lang.NoClassDefFoundError:在 android studio 3.6.1 中解析失败:Lcom/google/android/gms/common/R$string?
- kotlin - WebSocket 握手响应缺少 HTTP 状态行
- haskell - 使用镜头访问字段
- android - 构建调试时不使用 build.gradle 中的部分代码的条件
- google-sheets - FILTER() 与 Google 表格中其他列的完整性问题
- mysql - 为什么我必须在这里使用 group by?
- apache-kafka-connect - 如何配置 Confluent Kafka Connect 本地 S3 接收器
- angular - PWA 不适用于构建版本,仅适用于本地主机上的 prod 构建版本