Javascript JS Promise.all在循环内等待异步操作

Javascript JS Promise.all在循环内等待异步操作,javascript,promise,Javascript,Promise,我在下面的循环中进行2 DB调用,以获取项目的statsID和countryCode。我将它们都记录在循环中,结果很好。但是,在Promise.all()之后,存储的结果(名为带有附加数据的变量)永远不会得到安慰。它在某个地方卡住了,我不知道为什么。谢谢你的提示 const body = d.data const countriesWithNoCode = [] const withAdditionalData = await Promise.all(body.map(async (item)

我在下面的循环中进行2 DB调用,以获取项目的
statsID
countryCode
。我将它们都记录在循环中,结果很好。但是,在
Promise.all()之后,存储的结果(名为
带有附加数据的变量)永远不会得到安慰。它在某个地方卡住了,我不知道为什么。谢谢你的提示

const body = d.data
const countriesWithNoCode = []
const withAdditionalData = await Promise.all(body.map(async (item) => {
    //get stats_id
    const statsM = new statsModel()
    const statsID = await statsM.getStatsId(item.BeverageTypes.trim())
    console.log(statsID) // is fine

    //check if country can be found by 2-digit code
    const countryCode = await statsM.getCountryCode(item.Country.toString().trim());

    if(!countryCode){
        countriesWithNoCode.push(item.Country.toString().trim())
    }
    console.log(countryCode) // is fine
    return {item, item.countryCode: countryCode, item.statsId: statsID}
}))

//it never consoles the following / it never gets there
console.log('withAdditionalData') 
console.log(withAdditionalData) 

return{item,item.countryCode:countryCode,item.statsId:statsId}
你的意思是
return{item,countryCode:item.countryCode,statsId:item.statsId}
还是
return{…item,countryCode,statsId}?如果我使用排列运算符{…item,countryCode,statsId},它会告诉我“语法错误,意外标记”如果没有spread{item,countryCode:item.countryCode,statsID:item.statsID},它仍然不会使用“附加数据”进行控制台,它只是卡住了…尝试记录
body.length
,并确保所有迭代都返回一个实际已解决的承诺。是的,单个项目没有返回承诺。在我的模型中,只有当结果有一个长度时,我才返回DB结果,并且在DB中没有找到一个项,导致没有返回Primise。。。谢谢!