javascript - 响应在地图外没有变化
问题描述
我正在为我的大学项目开发一个应用程序。在这里,我需要运行很多请求以获取有关巴西一些交易所的信息。得到所有结果后,我需要将它们全部返回给用户。
router.get("/", async function(req, res, next) {
const response = {
exchanges: {
Msg: { dtTm: timeConverter() },
SctyHghstIncrLst: []
}
};
var exchanges_to_await = [
{ id: 775, symb: "VALE3", desc: "Vale S.A." },
{ id: 884, symb: "ITUB4", desc: "Itaú Unibanco" },
{ id: 69, symb: "BBDC4", desc: "Banco Bradesco SA Preference Shares" },
{ id: 1978, symb: "BBDC4", desc: "B3 SA - Brasil Bolsa Balcao" },
{ id: 484, symb: "PETR4", desc: "Petroleo Brasileiro SA Petrobras Preference Shares" },
];
try {
exchanges_to_await.map(async exchange => {
let await_response = await axios.get(
`https://api.cotacoes.uol.com/asset/intraday/list/?format=JSON&fields=price,pctChange,date&item=${exchange.id}&`
);
var exchange_done = {
symb: exchange.symb,
desc: exchange.desc,
SctyQtn: {
curPrc: await_response.data.docs[0].price,
prcFlcn: await_response.data.docs[0].pctChange
}
};
return response["exchanges"]["SctyHghstIncrLst"].push(exchange_done);
});
} catch (error) {
response["exchangesError"] = error;
}
res.send(response);
问题是因为响应没有更新。我认为范围很简单,但我不知道为什么它不起作用。你能帮我找到解决办法吗?
这是我得到的结果
{
"exchanges": {
"Msg": {
"dtTm": "13:36:25 26/2/2020"
},
"SctyHghstIncrLst": []
}
}
解决方案
尝试使用 Promise.all()
let promises = exchanges_to_await.map(async exchange => {
return axios.get('/https://api.cotacoes.uol.com/asset/intraday/list/?format=JSON&fields=price,pctChange,date&item=${exchange.id}&')
.then(function (await_response) {
console.log(await_response);
var exchange_done = {
symb: exchange.symb,
desc: exchange.desc,
SctyQtn: {
curPrc: await_response.data.docs[0].price,
prcFlcn: await_response.data.docs[0].pctChange
}
};
response["exchanges"]["SctyHghstIncrLst"].push(exchange_done);
})
.catch(function (error) {
// handle error
console.log(error);
})
})
Promise.all(promises).then(function (values) {
res.send(response);
})
推荐阅读
- javascript - 使用 $regex 查询 mongoose 不返回任何结果
- autodesk-forge - 通过 Autodesk Forge 将数据推送到云端
- c# - 如何创建/读取日历和事件 Azure 活动目录 [Microsoft.Graph]
- typescript - 为什么 A => B 类型的 fn 不允许 x => x?
- angular - Angular 6 SSR Sass 使用 Webpack 缩小
- graphics - 为什么缩放和平移的顺序对模型矩阵很重要?
- html - 使用 flexbox 在 DIV 内居中左对齐元素
- python - 通过python中的libvlc读取IP摄像头流并在opencv中处理帧
- python - 使用 JQuery 的 .load() 进入无限循环
- django - 使用 Django 和 websockets 进行跨站点请求伪造保护