【发布时间】:2021-06-23 05:49:42
【问题描述】:
我想将tripPrice 添加到我数组的每个对象中,但最后,tripPrice 是undefined
这是我的代码:
async getTest(req, res) {
let result = await this.model.Pricing.getTest(req.params);
result.map(async item => {
let params = {
originStation: item.fromStation,
destStation: item.toStation,
cityCode: 1,
carClass: item.carType,
}
let tripPrice = await axios.post(`${appConfig.gpsServer.host}/api/trip/price`, params);
item.price = tripPrice
return item
})
return result
}
如何在调用 API 后向我的对象添加新属性?它无需异步等待和 API 调用即可工作
【问题讨论】:
-
是
tripPrice是undefined还是在异步调用成功之前检查item.price? Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference 另见Use async await with Array.map -
你需要等待所有的承诺
await Promise.all(result.map(async item => {...
标签: javascript node.js ecmascript-6