首页 > 解决方案 > 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

标签: node.jsexpressknex.js

解决方案


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


推荐阅读