我有一个json对象,该对象可以有多个订单,并且彼此有多个订单购物车行:
{
"id": 4188,
"address": "test. test",
"hnr": "22",
"orders": [
{
"id": 5847,
"order_nr": "1212",
"orders_carts": [
{
"id": 14273,
"amount": 5,
"supplied": 3,
"backorder": 2,
},
{
"id": 1250,
"amount": 1,
"supplied": 1,
"backorder": 0,
}
]
}
]
}
我想要的是遍历所有订单,并在该遍历中遍历所有购物车行。我需要在购物车循环中获取一些附加信息(异步)。
但是我不知道该怎么做。
所以我以为我有一个功能a(遍历订单):
orderRows.forEach(function(entry) { //loop through all orders
readRows(entry)
})
还有一个读取购物车行的异步函数。
async function readRows(cart) {
for (const cart of entry.orders_carts) {
let result = await sequalize.models.part.getStock(cart.partId)
console.log(result)
}
}
但是,这不能按预期方式工作,因为在foreach循环中返回了promise。有什么好的解决方案?